From 058ee238ee549826d1ba06cb63f84c1d7052cdae Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Wed, 31 Jan 2007 11:06:44 +0000 Subject: [PATCH] Fix for Bug 172158 - [Content Assist] Macro proposals missing in preprocessor directives --- .../core/parser/scanner2/BaseScanner.java | 19 ++++- .../CompletionTest_MacroRef_NoPrefix.java | 4 +- .../CompletionTest_MacroRef_Prefix.java | 5 +- .../DOMCompletionContributor.java | 71 ++++++++++++++----- 4 files changed, 73 insertions(+), 26 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java index ae7bae90940..32e21c68ca6 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java @@ -9,6 +9,7 @@ * IBM Corporation - initial implementation * Markus Schorn (Wind River Systems) * Bryan Wilkinson (QNX) - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151207 + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.scanner2; @@ -1613,6 +1614,17 @@ abstract class BaseScanner implements IScanner { IToken result= null; try { result = fetchToken(); + } catch (OffsetLimitReachedException olre) { + if (contentAssistMode) { + IASTCompletionNode node= olre.getCompletionNode(); + if (node != null) { + result= newToken(IToken.tCOMPLETION, node.getCompletionPrefix().toCharArray()); + } else { + result= newToken(IToken.tCOMPLETION); + } + } else { + throw olre; + } } catch (ArrayIndexOutOfBoundsException e) { if (isCancelled) { throw new ParseError(ParseError.ParseErrorKind.TIMEOUT_OR_CANCELLED); @@ -2037,7 +2049,10 @@ abstract class BaseScanner implements IScanner { } // We've run out of contexts, our work is done here - return contentAssistMode ? new SimpleToken(IToken.tCOMPLETION, Integer.MAX_VALUE, null, Integer.MAX_VALUE) : null; + if (contentAssistMode) { + return new SimpleToken(IToken.tCOMPLETION, Integer.MAX_VALUE, null, Integer.MAX_VALUE); + } + return null; } protected CharTable ident = new CharTable(1024); @@ -4702,7 +4717,7 @@ abstract class BaseScanner implements IScanner { * @see org.eclipse.cdt.core.parser.IScanner#setContentAssistMode(int) */ public void setContentAssistMode(int offset) { - bufferLimit[0] = offset; + setOffsetBoundary(offset); contentAssistMode = true; } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_NoPrefix.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_NoPrefix.java index 110bbbcf47b..c1607109b65 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_NoPrefix.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_NoPrefix.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004 IBM Corporation and others. + * Copyright (c) 2007 IBM Corporation 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 @@ -38,8 +38,6 @@ public class CompletionTest_MacroRef_NoPrefix extends CompletionProposalsBaseTe public CompletionTest_MacroRef_NoPrefix(String name) { super(name); - // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172158 - setExpectFailure(172158); } public static Test suite() { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_Prefix.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_Prefix.java index 6105923f054..3e205286e54 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_Prefix.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_MacroRef_Prefix.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004 IBM Corporation and others. + * Copyright (c) 2004, 2007 IBM Corporation 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 @@ -27,14 +27,11 @@ public class CompletionTest_MacroRef_Prefix extends CompletionProposalsBaseTest private final String headerFileFullPath ="resources/contentassist/" + headerFileName; private final String expectedPrefix = "D"; private final String[] expectedResults = { - // missing result: "DEBUG" }; public CompletionTest_MacroRef_Prefix(String name) { super(name); - // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=172158 - setExpectFailure(172158); } public static Test suite() { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java index 558afcb2fc8..8c29be5dce8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java @@ -18,7 +18,10 @@ import java.util.Iterator; import java.util.List; import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.TextUtilities; import org.eclipse.swt.graphics.Image; import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; @@ -53,6 +56,7 @@ import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility; import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.text.ICPartitions; import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; @@ -66,8 +70,14 @@ public class DOMCompletionContributor implements ICompletionContributor { String prefix, List proposals) { - if (completionNode != null) { - boolean handleMacros = false; + if (completionNode == null) { + return; + } + if(inPreprocessorDirective(viewer.getDocument(), offset)) { + // add only macros + addMacroProposals(viewer, offset, completionNode, prefix, proposals); + } else { + boolean handleMacros= false; IASTName[] names = completionNode.getNames(); if (names == null || names.length == 0) // No names, not much we can do here @@ -80,7 +90,10 @@ public class DOMCompletionContributor implements ICompletionContributor { // The node isn't properly hooked up, must have backtracked out of this node continue; IBinding[] bindings = names[i].resolvePrefix(); - if (names[i].getParent() instanceof IASTIdExpression) handleMacros = true; + if (names[i].getParent() instanceof IASTIdExpression) { + // handle macros only if there is a prefix + handleMacros = prefix.length() > 0; + } if (bindings != null) for (int j = 0; j < bindings.length; ++j) { IBinding binding = bindings[j]; @@ -96,21 +109,45 @@ public class DOMCompletionContributor implements ICompletionContributor { IBinding binding = (IBinding)iBinding.next(); handleBinding(binding, completionNode, offset, viewer, proposals); } - - // Find all macros if there is a prefix - if (prefix.length() > 0 && handleMacros) { - IASTPreprocessorMacroDefinition[] macros = completionNode.getTranslationUnit().getMacroDefinitions(); - if (macros != null) - for (int i = 0; i < macros.length; ++i) - if (CharArrayUtils.equals(macros[i].getName().toCharArray(), 0, prefix.length(), prefix.toCharArray(), false)) - handleMacro(macros[i], completionNode, offset, viewer, proposals); - macros = completionNode.getTranslationUnit().getBuiltinMacroDefinitions(); - if (macros != null) - for (int i = 0; i < macros.length; ++i) - if (CharArrayUtils.equals(macros[i].getName().toCharArray(), 0, prefix.length(), prefix.toCharArray(), false)) - handleMacro(macros[i], completionNode, offset, viewer, proposals); + + if (handleMacros) { + addMacroProposals(viewer, offset, completionNode, prefix, proposals); } - } + } + } + + private void addMacroProposals(ITextViewer viewer, int offset, + ASTCompletionNode completionNode, String prefix, List proposals) { + char[] prefixChars= prefix.toCharArray(); + IASTPreprocessorMacroDefinition[] macros = completionNode.getTranslationUnit().getMacroDefinitions(); + if (macros != null) + for (int i = 0; i < macros.length; ++i) + if (CharArrayUtils.equals(macros[i].getName().toCharArray(), 0, prefixChars.length, prefixChars, false)) + handleMacro(macros[i], completionNode, offset, viewer, proposals); + macros = completionNode.getTranslationUnit().getBuiltinMacroDefinitions(); + if (macros != null) + for (int i = 0; i < macros.length; ++i) + if (CharArrayUtils.equals(macros[i].getName().toCharArray(), 0, prefixChars.length, prefixChars, false)) + handleMacro(macros[i], completionNode, offset, viewer, proposals); + } + + /** + * Check if given offset is inside a preprocessor directive. + * + * @param doc the document + * @param offset the offset to check + * @return true if offset is inside a preprocessor directive + */ + private boolean inPreprocessorDirective(IDocument doc, int offset) { + if (offset > 0 && offset == doc.getLength()) { + --offset; + } + try { + return ICPartitions.C_PREPROCESSOR + .equals(TextUtilities.getContentType(doc, ICPartitions.C_PARTITIONING, offset, false)); + } catch (BadLocationException exc) { + } + return false; } protected void handleBinding(IBinding binding, ASTCompletionNode completionNode, int offset, ITextViewer viewer, List proposals) {