1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 01:15:29 +02:00

Bug 412250: Tidy up.

This commit is contained in:
Andrew Gvozdev 2013-07-04 22:20:48 -04:00
parent d1e966b06e
commit 01bfa48d40
4 changed files with 72 additions and 86 deletions

View file

@ -55,14 +55,14 @@ public class OpenDeclarationAction extends TextEditorAction {
ITextSelection textSelection = (ITextSelection) provider.getSelection();
int offset = textSelection.getOffset();
WordPartDetector wordPart = new WordPartDetector(doc, offset);
String name = wordPart.toString();
if (WordPartDetector.inMacro(doc, offset)) {
String name = wordPart.getName();
if (wordPart.isMacro()) {
directives = makefile.getMacroDefinitions(name);
if (directives.length == 0) {
directives = makefile.getBuiltinMacroDefinitions(name);
}
} else if (wordPart.isIncludeDirective()) {
String incFile = wordPart.getIncludedFile();
String incFile = wordPart.getName();
incFile = makefile.expandString(incFile, true);
for (IDirective dir : makefile.getDirectives()) {
if (dir instanceof IInclude) {

View file

@ -10,30 +10,27 @@
*******************************************************************************/
package org.eclipse.cdt.make.internal.ui.text;
import org.eclipse.cdt.make.internal.core.makefile.gnu.GNUMakefileUtil;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
public class WordPartDetector {
IDocument document;
int offset;
String wordPart = ""; //$NON-NLS-1$
private IDocument document;
private int offset;
private String wordPart = ""; //$NON-NLS-1$
private WORDPART_TYPE type = WORDPART_TYPE.UNDETERMINED;
/**
* WordPartDetector.
* @param viewer is a text viewer
* @param documentOffset
*/
public WordPartDetector(ITextViewer viewer, int documentOffset) {
this(viewer.getDocument(), documentOffset);
private enum WORDPART_TYPE {
MACRO,
INCLUDE,
UNDETERMINED,
}
/**
*
* @param doc
* @param documentOffset
* @param doc - text document.
* @param documentOffset - cursor location in the document.
*/
public WordPartDetector(IDocument doc, int documentOffset) {
document = doc;
@ -53,72 +50,66 @@ public class WordPartDetector {
offset++;
wordPart = doc.get(offset, endOffset - offset);
} catch (BadLocationException e) {
// do nothing
MakeUIPlugin.log(e);
}
}
public static boolean inMacro(ITextViewer viewer, int offset) {
return inMacro(viewer.getDocument(), offset);
}
public static boolean inMacro(IDocument document, int offset) {
boolean isMacro = false;
// Try to figure out if we are in a Macro.
// Try to figure out if the cursor is on a macro.
try {
for (int index = offset - 1; index >= 0; index--) {
char c;
c = document.getChar(index);
if (c == '$') {
isMacro = true;
break;
type = WORDPART_TYPE.MACRO;
return;
} else if (Character.isWhitespace(c) || c == ')' || c == '}') {
break;
}
}
} catch (BadLocationException e) {
MakeUIPlugin.log(e);
}
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 to figure out if the cursor is on an include directive.
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;
}
if (GNUMakefileUtil.isInclude(line)) {
type = WORDPART_TYPE.INCLUDE;
int lineOffset = document.getLineOffset(lineNumber);
/**
* 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);
int position = offset - lineOffset;
String before = line.substring(0, position);
if (!(before + '.').trim().contains(" ")) { //$NON-NLS-1$
// the cursor is on the first word which is "include" keyword
// so find the second word which is the first include file
String sub = line.substring(line.indexOf(' ', position)).trim();
wordPart = sub.substring(0, sub.indexOf(' ')).trim();
} else {
wordPart = findWord(line, position);
}
return;
}
} catch (BadLocationException e) {
MakeUIPlugin.log(e);
}
return inc;
}
/**
* Check if the cursor is in macro.
*
* @return {@code true} if the cursor is located in macro, {@code false} otherwise.
*/
public boolean isMacro() {
return type == WORDPART_TYPE.MACRO;
}
/**
* 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() {
return type == WORDPART_TYPE.INCLUDE;
}
/**
@ -148,25 +139,20 @@ public class WordPartDetector {
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
public String toString() {
return wordPart;
}
public int getOffset() {
public String getName() {
return wordPart;
}
public int getOffset(){
return offset;
}
boolean isMakefileLetter(char c) {
private boolean isMakefileLetter(char c) {
return Character.isLetterOrDigit(c) || c == '_' || c == '.';
}
}

View file

@ -18,8 +18,8 @@ import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.core.makefile.IMacroDefinition;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IRule;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.internal.ui.MakeUIImages;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.internal.ui.text.CompletionProposalComparator;
import org.eclipse.cdt.make.internal.ui.text.WordPartDetector;
import org.eclipse.cdt.make.ui.IWorkingCopyManager;
@ -101,8 +101,8 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
WordPartDetector wordPart = new WordPartDetector(viewer, documentOffset);
boolean macro = WordPartDetector.inMacro(viewer, documentOffset);
WordPartDetector wordPart = new WordPartDetector(viewer.getDocument(), documentOffset);
boolean macro = wordPart.isMacro();
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
IDirective[] statements = null;
if (macro) {
@ -131,14 +131,14 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
image = imageTarget;
infoString = name;
}
if (name != null && name.startsWith(wordPart.toString())) {
if (name != null && name.startsWith(wordPart.getName())) {
IContextInformation info = new ContextInformation(name, infoString);
String displayString = (name.equals(infoString) ? name : name + " - " + infoString); //$NON-NLS-1$
ICompletionProposal result =
new CompletionProposal(
name,
wordPart.getOffset(),
wordPart.toString().length(),
wordPart.getName().length(),
name.length(),
image,
displayString,
@ -154,8 +154,8 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
@Override
public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
WordPartDetector wordPart = new WordPartDetector(viewer, documentOffset);
boolean macro = WordPartDetector.inMacro(viewer, documentOffset);
WordPartDetector wordPart = new WordPartDetector(viewer.getDocument(), documentOffset);
boolean macro = wordPart.isMacro();
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
ArrayList<String> contextList = new ArrayList<String>();
if (macro) {
@ -163,7 +163,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
for (IDirective statement : statements) {
if (statement instanceof IMacroDefinition) {
String name = ((IMacroDefinition) statement).getName();
if (name != null && name.equals(wordPart.toString())) {
if (name != null && name.equals(wordPart.getName())) {
String value = ((IMacroDefinition) statement).getValue().toString();
if (value != null && value.length() > 0) {
contextList.add(value);
@ -175,7 +175,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
for (IDirective statement : statements) {
if (statement instanceof IMacroDefinition) {
String name = ((IMacroDefinition) statement).getName();
if (name != null && name.equals(wordPart.toString())) {
if (name != null && name.equals(wordPart.getName())) {
String value = ((IMacroDefinition) statement).getValue().toString();
if (value != null && value.length() > 0) {
contextList.add(value);
@ -188,7 +188,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
IContextInformation[] result = new IContextInformation[contextList.size()];
for (int i = 0; i < result.length; i++) {
String context = contextList.get(i);
result[i] = new ContextInformation(imageMacro, wordPart.toString(), context);
result[i] = new ContextInformation(imageMacro, wordPart.getName(), context);
}
return result;

View file

@ -56,10 +56,10 @@ public class MakefileTextHover implements ITextHover {
IWorkingCopyManager fManager = MakeUIPlugin.getDefault().getWorkingCopyManager();
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
if (makefile != null) {
WordPartDetector wordPart = new WordPartDetector(textViewer, offset);
String name = wordPart.toString();
WordPartDetector wordPart = new WordPartDetector(textViewer.getDocument(), offset);
String name = wordPart.getName();
IMacroDefinition[] statements = null;
if (WordPartDetector.inMacro(textViewer, offset)) {
if (wordPart.isMacro()) {
statements = makefile.getMacroDefinitions(name);
if (statements == null || statements.length == 0) {
statements = makefile.getBuiltinMacroDefinitions(name);