diff options
author | Sami Wagiaalla | 2012-10-31 18:54:52 +0000 |
---|---|---|
committer | Camilo Bernal | 2012-11-15 20:43:15 +0000 |
commit | fb1001655548c43d86b94e9534fa3109b7c40c61 (patch) | |
tree | c8484d8ea13a53fab2627e2f85d6e5e5e0e7b699 | |
parent | 5f65e5cbdd6b2c8d811b68d9981c4ca838dbc357 (diff) | |
download | org.eclipse.linuxtools-fb1001655548c43d86b94e9534fa3109b7c40c61.tar.gz org.eclipse.linuxtools-fb1001655548c43d86b94e9534fa3109b7c40c61.tar.xz org.eclipse.linuxtools-fb1001655548c43d86b94e9534fa3109b7c40c61.zip |
Add global keyword completion
And
- Propose prope points only when preceded by 'probe'
- Complete first and second sections of probe points
independently to reduce noise.
- Remove dead code.
Change-Id: I05d44f90afa93cbb3d72882b707794bc60ecce1c
Reviewed-on: https://git.eclipse.org/r/8458
Tested-by: Hudson CI
Reviewed-by: Camilo Bernal <cabernal@redhat.com>
IP-Clean: Camilo Bernal <cabernal@redhat.com>
Tested-by: Camilo Bernal <cabernal@redhat.com>
5 files changed, 198 insertions, 44 deletions
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide.tests/src/org/eclipse/linuxtools/systemtap/ui/ide/test/editors/stp/STPCompletionProcessorTest.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide.tests/src/org/eclipse/linuxtools/systemtap/ui/ide/test/editors/stp/STPCompletionProcessorTest.java index 7ebafffcc2..5cde4ec7e1 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide.tests/src/org/eclipse/linuxtools/systemtap/ui/ide/test/editors/stp/STPCompletionProcessorTest.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide.tests/src/org/eclipse/linuxtools/systemtap/ui/ide/test/editors/stp/STPCompletionProcessorTest.java @@ -1,7 +1,9 @@ package org.eclipse.linuxtools.systemtap.ui.ide.test.editors.stp; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPCompletionProcessor; @@ -11,7 +13,7 @@ public class STPCompletionProcessorTest { private static String TEST_STP_SCRIPT = ""+ "\n"+ - "\n"+ + "\n//marker1"+ "probe syscall.write{\n"+ " // Some comment inside a probe\n"+ " printf(\"%s fd %d\taddr%d\tcount%dargstr%s\n\", name, fd, buf_uaddr, count, argstr)\n"+ @@ -36,5 +38,62 @@ public class STPCompletionProcessorTest { TEST_STP_SCRIPT.length()); assertNotNull(proposals); } - + + @Test + public void testGlobalCompletion() { + Document testDocument = new Document(TEST_STP_SCRIPT); + int offset = TEST_STP_SCRIPT.indexOf("//marker1"); + + STPCompletionProcessor completionProcessor = new STPCompletionProcessor(); + ICompletionProposal[] proposals = completionProcessor + .computeCompletionProposals(testDocument, + offset); + + assertTrue(proposalsContain(proposals, "probe ")); + assertTrue(proposalsContain(proposals, "global ")); + assertTrue(proposalsContain(proposals, "function ")); + } + + @Test + public void testGlobalPartialCompletion() throws BadLocationException { + Document testDocument = new Document(TEST_STP_SCRIPT); + int offset = TEST_STP_SCRIPT.indexOf("//marker1"); + String prefix = "prob"; + testDocument.replace(offset, 0, prefix); + offset += prefix.length(); + + STPCompletionProcessor completionProcessor = new STPCompletionProcessor(); + ICompletionProposal[] proposals = completionProcessor + .computeCompletionProposals(testDocument, + offset); + + assertTrue(proposalsContain(proposals, "probe ")); + assertTrue(!proposalsContain(proposals, "global ")); + assertTrue(!proposalsContain(proposals, "function ")); + } + + @Test + public void testGlobalInvalidCompletion() throws BadLocationException { + Document testDocument = new Document(TEST_STP_SCRIPT); + int offset = TEST_STP_SCRIPT.indexOf("//marker1"); + String prefix = "probe fake.fake"; + testDocument.replace(offset, 0, prefix); + offset += prefix.length(); + + STPCompletionProcessor completionProcessor = new STPCompletionProcessor(); + ICompletionProposal[] proposals = completionProcessor + .computeCompletionProposals(testDocument, + offset); + + assertTrue(proposalsContain(proposals, "No completion data found.")); + } + + private boolean proposalsContain(ICompletionProposal[] proposals, String proposal){ + for (ICompletionProposal p : proposals) { + if (p.getDisplayString().equals(proposal)) + return true; + } + return false; + } + } diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/Messages.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/Messages.java new file mode 100644 index 0000000000..a39f1f882e --- /dev/null +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/Messages.java @@ -0,0 +1,17 @@ +package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.messages"; //$NON-NLS-1$ + public static String STPCompletionProcessor_global; + public static String STPCompletionProcessor_probe; + public static String STPCompletionProcessor_function; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + } +} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPCompletionProcessor.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPCompletionProcessor.java index 0ccd34ab74..da48bbd437 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPCompletionProcessor.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPCompletionProcessor.java @@ -11,10 +11,14 @@ *******************************************************************************/ package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; +import java.util.ArrayList; + import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.ITypedRegion; import org.eclipse.jface.text.contentassist.CompletionProposal; +import org.eclipse.jface.text.contentassist.ContextInformation; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContentAssistProcessor; import org.eclipse.jface.text.contentassist.IContextInformation; @@ -26,6 +30,15 @@ public class STPCompletionProcessor implements IContentAssistProcessor { private final char[] PROPOSAL_ACTIVATION_CHARS = new char[] { '.' }; private ICompletionProposal[] NO_COMPLETIONS = new ICompletionProposal[0]; + private static final String GLOBAL_KEYWORD = "global "; //$NON-NLS-1$ + private static final String PROBE_KEYWORD = "probe "; //$NON-NLS-1$ + private static final String FUNCTION_KEYWORD = "function "; //$NON-NLS-1$ + + private static final String[][] GLOBAL_KEYWORDS = { + { GLOBAL_KEYWORD, Messages.STPCompletionProcessor_global }, + { PROBE_KEYWORD, Messages.STPCompletionProcessor_probe }, + { FUNCTION_KEYWORD, Messages.STPCompletionProcessor_function } }; + /* (non-Javadoc) * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int) */ @@ -36,59 +49,104 @@ public class STPCompletionProcessor implements IContentAssistProcessor { public ICompletionProposal[] computeCompletionProposals(IDocument document, int offset){ - String prefix; - int locationOffset =0; + ITypedRegion partition = null; + + try { + partition = document.getPartition(offset); + } catch (BadLocationException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + String prefix = ""; //$NON-NLS-1$ + String prePrefix = ""; //$NON-NLS-1$ // Get completion hint from document try { - prefix = completionWord(document, offset); - locationOffset = completionReplaceWordLocation(document, offset); + prefix = getPrefix(document, offset); + prePrefix = getPrecedingToken(document, prefix, offset); } catch (BadLocationException e) { return NO_COMPLETIONS; } - // If cannot find a place to replace our partial typed completion - // with the full one, abort with no completions. - if (locationOffset < 0) - return NO_COMPLETIONS; - - String[] completionData = STPMetadataSingleton.getCompletionResults(prefix); + if (prePrefix.startsWith("probe")){ //$NON-NLS-1$ + return getProbeCompletionList(prefix, offset); + } - // If cannot find any completions - // abort with no completions. - if (completionData.length < 1) - return NO_COMPLETIONS; + // In the global scope return global keyword completion. + if (partition.getType() == IDocument.DEFAULT_CONTENT_TYPE ){ + return getGlobalKeywordCompletion(prefix, offset); + } + + return NO_COMPLETIONS; + } + private ICompletionProposal[] getProbeCompletionList(String prefix, int offset){ + String[] completionData = STPMetadataSingleton.getCompletionResults(prefix); + // get the last section of the prefix + int i = prefix.indexOf('.'); + if (i > 0){ + prefix = prefix.substring(i+1); + } + return buildCompletionList(offset, prefix.length(), completionData); + + } + + private ICompletionProposal[] buildCompletionList(int offset, int prefixLength,String[] completionData){ // Build proposals and submit ICompletionProposal[] result = new ICompletionProposal[completionData.length]; for (int i = 0; i < completionData.length; i++) result[i] = new CompletionProposal( - completionData[i].substring(offset - locationOffset), - offset, 0, completionData[i].length(), null, - completionData[i], null, null); + completionData[i].substring(prefixLength), + offset, + 0, + completionData[i].length() - prefixLength, + null, + completionData[i], + null, + null); return result; + + } + + private ICompletionProposal[] getGlobalKeywordCompletion(String prefix, int offset) { + + ArrayList<ICompletionProposal> completions = new ArrayList<ICompletionProposal>(); + int prefixLength = prefix.length(); + for (String[] keyword : GLOBAL_KEYWORDS) { + if (keyword[0].startsWith(prefix)){ + CompletionProposal proposal = new CompletionProposal( + keyword[0].substring(prefixLength), + offset, + 0, + keyword[0].length() - prefixLength, + null, + keyword[0], + new ContextInformation("contextDisplayString", "informationDisplayString"), //$NON-NLS-1$ //$NON-NLS-2$ + keyword[1]); + completions.add(proposal); + } + } + return completions.toArray(new ICompletionProposal[0]); } /** - * - * Compute location of completion proposal insertion. - * - * @param doc - document to insert completion. - * @param offset - offset of where completion hint was first generated. - * @return - offset into document for completion proposal insertion. - * @throws BadLocationException - * + * Returns the token preceding the current completion position. + * @param doc The document for the which the completion is requested. + * @param prefix The prefix for which the user has requested completion. + * @param offset Current offset in the document + * @return The preceding token. + * @throws BadLocationException */ - private int completionReplaceWordLocation(IDocument doc, int offset) - throws BadLocationException { - for (int n = offset - 1; n >= 0; n--) { - if (doc.getChar(n) == '.') - return n + 1; + private String getPrecedingToken(IDocument doc, String prefix, int offset) throws BadLocationException{ + // Skip trailing space + int n = offset - prefix.length() - 1; + while (n >= 0 && Character.isSpaceChar(doc.getChar(n))){ + n--; } - - return -1; + return getPrefix(doc, n + 1); } - + /** * * Return the word the user wants to submit for completion proposals. @@ -99,17 +157,14 @@ public class STPCompletionProcessor implements IContentAssistProcessor { * * @throws BadLocationException */ - private String completionWord(IDocument doc, int offset) + private String getPrefix(IDocument doc, int offset) throws BadLocationException { for (int n = offset - 1; n >= 0; n--) { char c = doc.getChar(n); if ((Character.isSpaceChar(c)) || (c == '\n') || (c == '\0')) { String word = doc.get(n + 1, offset - n - 1); - if (!word.isEmpty() && word.charAt(word.length() - 1) == '.') - return word.substring(0, word.length() - 1); - else - return word; + return word; } } return ""; //$NON-NLS-1$ @@ -149,6 +204,6 @@ public class STPCompletionProcessor implements IContentAssistProcessor { */ public String getErrorMessage() { // TODO: When does this trigger? - return "Error."; + return "Error."; //$NON-NLS-1$ } }
\ No newline at end of file diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPMetadataSingleton.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPMetadataSingleton.java index 0a74165b06..d4b8f25f9c 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPMetadataSingleton.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPMetadataSingleton.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; +import java.util.Set; /** @@ -60,7 +61,7 @@ public class STPMetadataSingleton { * */ public static String[] getCompletionResults(String match) { - + ArrayList<String> data = new ArrayList<String>(); // TODO: Until an error strategy is devised to better inform @@ -77,6 +78,10 @@ public class STPMetadataSingleton { /// narrow down the list with partial probe matches. if (tapsetAndProbeIncluded) { ArrayList<String> temp = builtMetadata.get(getTapset(match)); + + if (temp == null) + return new String[] {"No completion data found."}; + String probe = getTapsetProbe(match); for (int i=0; i<temp.size(); i++) { if (temp.get(i).startsWith(probe)) { @@ -85,8 +90,14 @@ public class STPMetadataSingleton { } } // If the result was a <tapset>, return all <probe> matches. - else - data = builtMetadata.get(match); + else{ + Set<String> tapset = builtMetadata.keySet(); + for (String probePoint : tapset) { + if (probePoint.startsWith(match)){ + data.add(probePoint); + } + } + } if (data == null) return new String[] {}; diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/messages.properties b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/messages.properties new file mode 100644 index 0000000000..89404bafa4 --- /dev/null +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/messages.properties @@ -0,0 +1,12 @@ +STPCompletionProcessor_global=Create a global variable.\n\ +e.g.\n\ +\ \ global foo +STPCompletionProcessor_probe=Create a systemtap probe \n\ +e.g.\n\ +\ \ probe syscall.write{\n\ +\ \ \ \ //...\n\ +\ \ } +STPCompletionProcessor_function=Create a function which can be called from other probes\ne.g.\n\ +\ \ function print_bar(){\n\ +\ \ \ \ printf("bar")\n\ +\ \ } |