Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3aae53472c2c6dc86c86c396c668dea8818459f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.internal.tcf.debug.launch;

import java.io.File;
import java.util.ArrayList;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupParticipant;
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
import org.eclipse.tm.tcf.services.ILineNumbers;

/**
 * The TCF source lookup participant knows how to translate a ILineNumbers.CodeArea
 * into a source file name
 */
public class TCFSourceLookupParticipant extends AbstractSourceLookupParticipant {

    public String getSourceName(Object object) throws CoreException {
        if (object instanceof String) {
            return (String)object;
        }
        if (object instanceof ILineNumbers.CodeArea) {
            ILineNumbers.CodeArea area = (ILineNumbers.CodeArea)object;
            // TODO: map file path from remote file system to local
            if (area.directory != null && area.file != null) {
                String d = area.directory;
                if (d.startsWith("/cygdrive/")) {
                    d = d.substring(10, 11) + ":" + d.substring(11);
                }
                return new File(d, area.file).getAbsolutePath();
            }
            return area.file;
        }
        return null;
    }

    @Override
    public Object[] findSourceElements(Object object) throws CoreException {
        String name = getSourceName(object);
        if (name != null) {
            IPath path = new Path(name);
            if (path.isAbsolute()) {
                IFile[] arr = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(path);
                if (arr != null && arr.length > 0) return arr;
            }
        }
        Object[] res = super.findSourceElements(name);
        if (name != null && (res == null || res.length == 0)) {
            // Remove file path and search by file base name
            String base = name;
            int i = name.lastIndexOf('/');
            int j = name.lastIndexOf('\\');
            if (i > j) base = name.substring(i + 1);
            if (j > i) base = name.substring(j + 1);
            res = super.findSourceElements(base);
        }
        ArrayList<Object> list = new ArrayList<Object>();
        for (Object o : res) {
            if (o instanceof LocalFileStorage) {
                IPath path = ((LocalFileStorage)o).getFullPath();
                IFile[] arr = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(path);
                if (arr != null && arr.length > 0) {
                    for (Object x : arr) list.add(x);
                    continue;
                }
            }
            list.add(o);
        }
        return list.toArray(new Object[list.size()]);
    }
}

Back to the top