1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-05 07:15:39 +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(); ITextSelection textSelection = (ITextSelection) provider.getSelection();
int offset = textSelection.getOffset(); int offset = textSelection.getOffset();
WordPartDetector wordPart = new WordPartDetector(doc, offset); WordPartDetector wordPart = new WordPartDetector(doc, offset);
String name = wordPart.toString(); String name = wordPart.getName();
if (WordPartDetector.inMacro(doc, offset)) { if (wordPart.isMacro()) {
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()) { } else if (wordPart.isIncludeDirective()) {
String incFile = wordPart.getIncludedFile(); String incFile = wordPart.getName();
incFile = makefile.expandString(incFile, true); incFile = makefile.expandString(incFile, true);
for (IDirective dir : makefile.getDirectives()) { for (IDirective dir : makefile.getDirectives()) {
if (dir instanceof IInclude) { if (dir instanceof IInclude) {

View file

@ -10,30 +10,27 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.make.internal.ui.text; 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.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;
public class WordPartDetector { public class WordPartDetector {
IDocument document; private IDocument document;
int offset; private int offset;
String wordPart = ""; //$NON-NLS-1$ private String wordPart = ""; //$NON-NLS-1$
private WORDPART_TYPE type = WORDPART_TYPE.UNDETERMINED;
/** private enum WORDPART_TYPE {
* WordPartDetector. MACRO,
* @param viewer is a text viewer INCLUDE,
* @param documentOffset UNDETERMINED,
*/
public WordPartDetector(ITextViewer viewer, int documentOffset) {
this(viewer.getDocument(), documentOffset);
} }
/** /**
* * @param doc - text document.
* @param doc * @param documentOffset - cursor location in the document.
* @param documentOffset
*/ */
public WordPartDetector(IDocument doc, int documentOffset) { public WordPartDetector(IDocument doc, int documentOffset) {
document = doc; document = doc;
@ -53,72 +50,66 @@ public class WordPartDetector {
offset++; offset++;
wordPart = doc.get(offset, endOffset - offset); wordPart = doc.get(offset, endOffset - offset);
} catch (BadLocationException e) { } catch (BadLocationException e) {
// do nothing MakeUIPlugin.log(e);
} }
} // Try to figure out if the cursor is on a macro.
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 { try {
for (int index = offset - 1; index >= 0; index--) { for (int index = offset - 1; index >= 0; index--) {
char c; char c;
c = document.getChar(index); c = document.getChar(index);
if (c == '$') { if (c == '$') {
isMacro = true; type = WORDPART_TYPE.MACRO;
break; return;
} else if (Character.isWhitespace(c) || c == ')' || c == '}') { } else if (Character.isWhitespace(c) || c == ')' || c == '}') {
break; break;
} }
} }
} catch (BadLocationException e) { } catch (BadLocationException e) {
MakeUIPlugin.log(e);
} }
return isMacro;
}
/** // Try to figure out if the cursor is on an include directive.
* 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 { try {
int lineNumber = document.getLineOfOffset(offset); int lineNumber = document.getLineOfOffset(offset);
String line = document.get(document.getLineOffset(lineNumber), document.getLineLength(lineNumber)); String line = document.get(document.getLineOffset(lineNumber), document.getLineLength(lineNumber));
String firstWord = findWord(line.trim(), 1); if (GNUMakefileUtil.isInclude(line)) {
isIncludeDirective = isIncludeKeyword(firstWord); type = WORDPART_TYPE.INCLUDE;
} catch (BadLocationException e) { int lineOffset = document.getLineOffset(lineNumber);
MakeUIPlugin.log(e);
}
return isIncludeDirective;
}
/** int position = offset - lineOffset;
* Gets include file name under the cursor. String before = line.substring(0, position);
* if (!(before + '.').trim().contains(" ")) { //$NON-NLS-1$
* @return include file name to which the cursor location corresponds. // the cursor is on the first word which is "include" keyword
*/ // so find the second word which is the first include file
public String getIncludedFile() { String sub = line.substring(line.indexOf(' ', position)).trim();
String inc = null; wordPart = sub.substring(0, sub.indexOf(' ')).trim();
try { } else {
int lineNumber = document.getLineOfOffset(offset); wordPart = findWord(line, position);
int lineOffset = document.getLineOffset(lineNumber); }
String line = document.get(lineOffset, document.getLineLength(lineNumber)); return;
int position = offset -lineOffset;
inc = findWord(line, position);
if (isIncludeKeyword(inc)) {
inc = findWord(line, line.indexOf(inc) + inc.length() + 1);
} }
} catch (BadLocationException e) { } catch (BadLocationException e) {
MakeUIPlugin.log(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; 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;
} }
public int getOffset() { public String getName() {
return wordPart;
}
public int getOffset(){
return offset; return offset;
} }
boolean isMakefileLetter(char c) { private boolean isMakefileLetter(char c) {
return Character.isLetterOrDigit(c) || c == '_' || 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.IMacroDefinition;
import org.eclipse.cdt.make.core.makefile.IMakefile; import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IRule; 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.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.CompletionProposalComparator;
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;
@ -101,8 +101,8 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
@Override @Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
WordPartDetector wordPart = new WordPartDetector(viewer, documentOffset); WordPartDetector wordPart = new WordPartDetector(viewer.getDocument(), documentOffset);
boolean macro = WordPartDetector.inMacro(viewer, documentOffset); boolean macro = wordPart.isMacro();
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput()); IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
IDirective[] statements = null; IDirective[] statements = null;
if (macro) { if (macro) {
@ -131,14 +131,14 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
image = imageTarget; image = imageTarget;
infoString = name; infoString = name;
} }
if (name != null && name.startsWith(wordPart.toString())) { if (name != null && name.startsWith(wordPart.getName())) {
IContextInformation info = new ContextInformation(name, infoString); IContextInformation info = new ContextInformation(name, infoString);
String displayString = (name.equals(infoString) ? name : name + " - " + infoString); //$NON-NLS-1$ String displayString = (name.equals(infoString) ? name : name + " - " + infoString); //$NON-NLS-1$
ICompletionProposal result = ICompletionProposal result =
new CompletionProposal( new CompletionProposal(
name, name,
wordPart.getOffset(), wordPart.getOffset(),
wordPart.toString().length(), wordPart.getName().length(),
name.length(), name.length(),
image, image,
displayString, displayString,
@ -154,8 +154,8 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
@Override @Override
public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
WordPartDetector wordPart = new WordPartDetector(viewer, documentOffset); WordPartDetector wordPart = new WordPartDetector(viewer.getDocument(), documentOffset);
boolean macro = WordPartDetector.inMacro(viewer, documentOffset); boolean macro = wordPart.isMacro();
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput()); IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
ArrayList<String> contextList = new ArrayList<String>(); ArrayList<String> contextList = new ArrayList<String>();
if (macro) { if (macro) {
@ -163,7 +163,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
for (IDirective statement : statements) { for (IDirective statement : statements) {
if (statement instanceof IMacroDefinition) { if (statement instanceof IMacroDefinition) {
String name = ((IMacroDefinition) statement).getName(); 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(); String value = ((IMacroDefinition) statement).getValue().toString();
if (value != null && value.length() > 0) { if (value != null && value.length() > 0) {
contextList.add(value); contextList.add(value);
@ -175,7 +175,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
for (IDirective statement : statements) { for (IDirective statement : statements) {
if (statement instanceof IMacroDefinition) { if (statement instanceof IMacroDefinition) {
String name = ((IMacroDefinition) statement).getName(); 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(); String value = ((IMacroDefinition) statement).getValue().toString();
if (value != null && value.length() > 0) { if (value != null && value.length() > 0) {
contextList.add(value); contextList.add(value);
@ -188,7 +188,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
IContextInformation[] result = new IContextInformation[contextList.size()]; IContextInformation[] result = new IContextInformation[contextList.size()];
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
String context = contextList.get(i); String context = contextList.get(i);
result[i] = new ContextInformation(imageMacro, wordPart.toString(), context); result[i] = new ContextInformation(imageMacro, wordPart.getName(), context);
} }
return result; return result;

View file

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