mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-20 07:25:23 +02:00
Bug 412250: Makefile Editor won't open include with F3
This commit is contained in:
parent
54665cdb60
commit
d1e966b06e
3 changed files with 112 additions and 8 deletions
|
@ -523,7 +523,7 @@ public class GNUMakefile extends AbstractMakefile implements IGNUMakefile {
|
||||||
// ignore the "include" keyword.
|
// ignore the "include" keyword.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
filenames[i - 1] = st.nextToken();
|
filenames[i - 1] = expandString(st.nextToken(), true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
filenames = new String[0];
|
filenames = new String[0];
|
||||||
|
|
|
@ -10,8 +10,12 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.make.internal.ui.editor;
|
package org.eclipse.cdt.make.internal.ui.editor;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.eclipse.cdt.make.core.makefile.IDirective;
|
import org.eclipse.cdt.make.core.makefile.IDirective;
|
||||||
import org.eclipse.cdt.make.core.makefile.IMakefile;
|
import org.eclipse.cdt.make.core.makefile.IMakefile;
|
||||||
|
import org.eclipse.cdt.make.core.makefile.gnu.IInclude;
|
||||||
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
|
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
|
||||||
import org.eclipse.cdt.make.internal.ui.text.WordPartDetector;
|
import org.eclipse.cdt.make.internal.ui.text.WordPartDetector;
|
||||||
import org.eclipse.cdt.make.ui.IWorkingCopyManager;
|
import org.eclipse.cdt.make.ui.IWorkingCopyManager;
|
||||||
|
@ -50,13 +54,36 @@ public class OpenDeclarationAction extends TextEditorAction {
|
||||||
try {
|
try {
|
||||||
ITextSelection textSelection = (ITextSelection) provider.getSelection();
|
ITextSelection textSelection = (ITextSelection) provider.getSelection();
|
||||||
int offset = textSelection.getOffset();
|
int offset = textSelection.getOffset();
|
||||||
WordPartDetector wordPart = new WordPartDetector(doc, textSelection.getOffset());
|
WordPartDetector wordPart = new WordPartDetector(doc, offset);
|
||||||
String name = wordPart.toString();
|
String name = wordPart.toString();
|
||||||
if (WordPartDetector.inMacro(doc, offset)) {
|
if (WordPartDetector.inMacro(doc, offset)) {
|
||||||
directives = makefile.getMacroDefinitions(name);
|
directives = makefile.getMacroDefinitions(name);
|
||||||
if (directives.length == 0) {
|
if (directives.length == 0) {
|
||||||
directives = makefile.getBuiltinMacroDefinitions(name);
|
directives = makefile.getBuiltinMacroDefinitions(name);
|
||||||
}
|
}
|
||||||
|
} else if (wordPart.isIncludeDirective()) {
|
||||||
|
String incFile = wordPart.getIncludedFile();
|
||||||
|
incFile = makefile.expandString(incFile, true);
|
||||||
|
for (IDirective dir : makefile.getDirectives()) {
|
||||||
|
if (dir instanceof IInclude) {
|
||||||
|
IInclude includeDirective = (IInclude) dir;
|
||||||
|
if (Arrays.asList(((IInclude) dir).getFilenames()).contains(incFile)) {
|
||||||
|
IDirective[] includedMakefiles = includeDirective.getDirectives();
|
||||||
|
for (IDirective includedMakefileDir : includedMakefiles) {
|
||||||
|
if (includedMakefileDir instanceof IMakefile) {
|
||||||
|
IMakefile includedMakefile = (IMakefile) includedMakefileDir;
|
||||||
|
URI uri = includedMakefile.getFileURI();
|
||||||
|
// endsWith() is potentially inaccurate but IInclude does not provide better way
|
||||||
|
if (uri != null && uri.toString().endsWith(incFile)) {
|
||||||
|
directives = new IDirective[1];
|
||||||
|
directives[0] = includedMakefileDir;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
directives = makefile.getTargetRules(name);
|
directives = makefile.getTargetRules(name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,22 +10,21 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.make.internal.ui.text;
|
package org.eclipse.cdt.make.internal.ui.text;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
import org.eclipse.jface.text.IRegion;
|
import org.eclipse.jface.text.IRegion;
|
||||||
import org.eclipse.jface.text.ITextViewer;
|
import org.eclipse.jface.text.ITextViewer;
|
||||||
|
|
||||||
/**
|
|
||||||
* Used to scan and detect for SQL keywords
|
|
||||||
*/
|
|
||||||
public class WordPartDetector {
|
public class WordPartDetector {
|
||||||
String wordPart = ""; //$NON-NLS-1$
|
IDocument document;
|
||||||
int offset;
|
int offset;
|
||||||
|
String wordPart = ""; //$NON-NLS-1$
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WordPartDetector.
|
* WordPartDetector.
|
||||||
* @param viewer is a text viewer
|
* @param viewer is a text viewer
|
||||||
* @param documentOffset into the SQL document
|
* @param documentOffset
|
||||||
*/
|
*/
|
||||||
public WordPartDetector(ITextViewer viewer, int documentOffset) {
|
public WordPartDetector(ITextViewer viewer, int documentOffset) {
|
||||||
this(viewer.getDocument(), documentOffset);
|
this(viewer.getDocument(), documentOffset);
|
||||||
|
@ -37,6 +36,7 @@ public class WordPartDetector {
|
||||||
* @param documentOffset
|
* @param documentOffset
|
||||||
*/
|
*/
|
||||||
public WordPartDetector(IDocument doc, int documentOffset) {
|
public WordPartDetector(IDocument doc, int documentOffset) {
|
||||||
|
document = doc;
|
||||||
offset = documentOffset - 1;
|
offset = documentOffset - 1;
|
||||||
int endOffset = documentOffset;
|
int endOffset = documentOffset;
|
||||||
try {
|
try {
|
||||||
|
@ -71,7 +71,7 @@ public class WordPartDetector {
|
||||||
if (c == '$') {
|
if (c == '$') {
|
||||||
isMacro = true;
|
isMacro = true;
|
||||||
break;
|
break;
|
||||||
} else if (Character.isWhitespace(c)) {
|
} else if (Character.isWhitespace(c) || c == ')' || c == '}') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,6 +80,83 @@ public class WordPartDetector {
|
||||||
return isMacro;
|
return isMacro;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quick check if the cursor sits on an include directive line.
|
||||||
|
*
|
||||||
|
* @return {@code true} if the cursor is located on include line, {@code false} otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isIncludeDirective() {
|
||||||
|
boolean isIncludeDirective = false;
|
||||||
|
try {
|
||||||
|
int lineNumber = document.getLineOfOffset(offset);
|
||||||
|
String line = document.get(document.getLineOffset(lineNumber), document.getLineLength(lineNumber));
|
||||||
|
String firstWord = findWord(line.trim(), 1);
|
||||||
|
isIncludeDirective = isIncludeKeyword(firstWord);
|
||||||
|
} catch (BadLocationException e) {
|
||||||
|
MakeUIPlugin.log(e);
|
||||||
|
}
|
||||||
|
return isIncludeDirective;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets include file name under the cursor.
|
||||||
|
*
|
||||||
|
* @return include file name to which the cursor location corresponds.
|
||||||
|
*/
|
||||||
|
public String getIncludedFile() {
|
||||||
|
String inc = null;
|
||||||
|
try {
|
||||||
|
int lineNumber = document.getLineOfOffset(offset);
|
||||||
|
int lineOffset = document.getLineOffset(lineNumber);
|
||||||
|
String line = document.get(lineOffset, document.getLineLength(lineNumber));
|
||||||
|
|
||||||
|
int position = offset -lineOffset;
|
||||||
|
inc = findWord(line, position);
|
||||||
|
if (isIncludeKeyword(inc)) {
|
||||||
|
inc = findWord(line, line.indexOf(inc) + inc.length() + 1);
|
||||||
|
}
|
||||||
|
} catch (BadLocationException e) {
|
||||||
|
MakeUIPlugin.log(e);
|
||||||
|
}
|
||||||
|
return inc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find word located in the given position. A word is defined here as a sequence of non-space characters.
|
||||||
|
*/
|
||||||
|
private static String findWord(String line, int position) {
|
||||||
|
String firstHalf;
|
||||||
|
try {
|
||||||
|
firstHalf = line.substring(0, position);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
firstHalf = line;
|
||||||
|
}
|
||||||
|
String secondHalf;
|
||||||
|
try {
|
||||||
|
secondHalf = line.substring(position);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
secondHalf = ""; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
int startIndex = firstHalf.lastIndexOf(' ') + 1;
|
||||||
|
int firstHalfLen = firstHalf.length();
|
||||||
|
int endIndex = firstHalfLen + secondHalf.indexOf(' ');
|
||||||
|
if (endIndex < firstHalfLen) {
|
||||||
|
endIndex = line.length();
|
||||||
|
}
|
||||||
|
// trim() gets rid of trailing end of line if captured
|
||||||
|
String word = line.substring(startIndex, endIndex).trim();
|
||||||
|
return word;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the string is include keyword.
|
||||||
|
*/
|
||||||
|
private static boolean isIncludeKeyword(String inc) {
|
||||||
|
@SuppressWarnings("nls")
|
||||||
|
boolean isKeyword = "include".equals(inc) || "-include".equals(inc) || "sinclude".equals(inc);
|
||||||
|
return isKeyword;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return wordPart;
|
return wordPart;
|
||||||
|
|
Loading…
Add table
Reference in a new issue