Class SourceCompletionProvider

  • All Implemented Interfaces:
    org.fife.ui.autocomplete.CompletionProvider

    class SourceCompletionProvider
    extends org.fife.ui.autocomplete.DefaultCompletionProvider
    Parses a Java AST for code completions. It currently scans the following:
    • Import statements
    • Method names
    • Field names
    Also, if the caret is inside a method, local variables up to the caret position are also returned.
    Version:
    1.0
    • Nested Class Summary

      • Nested classes/interfaces inherited from class org.fife.ui.autocomplete.AbstractCompletionProvider

        org.fife.ui.autocomplete.AbstractCompletionProvider.CaseInsensitiveComparator
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private JarManager jarManager
      Used to get information about what classes match imports.
      private static java.lang.String JAVA_LANG_PACKAGE  
      private JavaCompletionProvider javaProvider
      The parent completion provider.
      private ShorthandCompletionCache shorthandCache  
      private static java.lang.String THIS  
      • Fields inherited from class org.fife.ui.autocomplete.DefaultCompletionProvider

        seg
      • Fields inherited from class org.fife.ui.autocomplete.AbstractCompletionProvider

        comparator, completions
      • Fields inherited from class org.fife.ui.autocomplete.CompletionProviderBase

        EMPTY_STRING
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addCompletionsForExtendedClass​(java.util.Set<org.fife.ui.autocomplete.Completion> set, CompilationUnit cu, ClassFile cf, java.lang.String pkg, java.util.Map<java.lang.String,​java.lang.String> typeParamMap)
      Adds completions for accessible methods and fields of super classes.
      private void addCompletionsForLocalVarsMethods​(CompilationUnit cu, LocalVariable var, java.util.Set<org.fife.ui.autocomplete.Completion> retVal)
      Adds completions for all methods and public fields of a local variable.
      private void addCompletionsForStaticMembers​(java.util.Set<org.fife.ui.autocomplete.Completion> set, CompilationUnit cu, ClassFile cf, java.lang.String pkg)  
      void addJar​(LibraryInfo info)
      Adds a jar to read from.
      private void addLocalVarCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set, CodeBlock block, int offs)
      Adds completions for local variables in a code block inside a method.
      private void addLocalVarCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set, Method method, int offs)
      Adds completions for local variables in a method.
      private void addShorthandCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set)
      Adds simple shorthand completions relevant to Java.
      private boolean checkStringLiteralMember​(javax.swing.text.JTextComponent comp, java.lang.String alreadyEntered, CompilationUnit cu, java.util.Set<org.fife.ui.autocomplete.Completion> set)
      Checks whether the user is typing a completion for a String member after a String literal.
      void clearJars()
      Removes all jars from the "build path".
      private java.util.Map<java.lang.String,​java.lang.String> createTypeParamMap​(Type type, ClassFile cf)
      Creates and returns a mapping of type parameters to type arguments.
      private ClassFile getClassFileFor​(CompilationUnit cu, java.lang.String className)
      Gets the ClassFile for a class.
      java.util.List<org.fife.ui.autocomplete.Completion> getCompletionsAt​(javax.swing.text.JTextComponent tc, java.awt.Point p)  
      protected java.util.List<org.fife.ui.autocomplete.Completion> getCompletionsImpl​(javax.swing.text.JTextComponent comp)  
      java.util.List<LibraryInfo> getJars()
      Returns the jars on the "build path".
      SourceLocation getSourceLocForClass​(java.lang.String className)  
      private boolean isAccessible​(MemberInfo info, java.lang.String pkg)
      Returns whether a method defined by a super class is accessible to this class.
      protected boolean isValidChar​(char ch)  
      private void loadCompletionsForCaretPosition​(CompilationUnit cu, javax.swing.text.JTextComponent comp, java.lang.String alreadyEntered, java.util.Set<org.fife.ui.autocomplete.Completion> retVal)
      Loads completions based on the current caret location in the source.
      private void loadCompletionsForCaretPosition​(CompilationUnit cu, javax.swing.text.JTextComponent comp, java.lang.String alreadyEntered, java.util.Set<org.fife.ui.autocomplete.Completion> retVal, TypeDeclaration td, java.lang.String prefix, int caret)
      Loads completions based on the current caret location in the source.
      private void loadCompletionsForCaretPositionQualified​(CompilationUnit cu, java.lang.String alreadyEntered, java.util.Set<org.fife.ui.autocomplete.Completion> retVal, TypeDeclaration td, Method currentMethod, java.lang.String prefix, int offs)
      Loads completions for the text at the current caret position, if there is a "prefix" of chars and at least one '.' character in the text up to the caret.
      private void loadCompletionsForCaretPositionQualifiedCodeBlock​(CompilationUnit cu, java.util.Set<org.fife.ui.autocomplete.Completion> retVal, TypeDeclaration td, CodeBlock block, java.lang.String prefix, int offs)  
      private void loadCompletionsForImport​(java.util.Set<org.fife.ui.autocomplete.Completion> set, java.lang.String importStr, java.lang.String pkgName)
      Loads completions for a single import statement.
      private void loadImportCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set, java.lang.String text, CompilationUnit cu)
      Loads completions for all import statements.
      boolean removeJar​(java.io.File jar)
      Removes a jar from the "build path".
      (package private) void setJavaProvider​(JavaCompletionProvider javaProvider)
      Sets the parent Java provider.
      void setShorthandCache​(ShorthandCompletionCache shorthandCache)
      Set template completion cache for source completion provider.
      • Methods inherited from class org.fife.ui.autocomplete.DefaultCompletionProvider

        getAlreadyEnteredText, getParameterizedCompletions, init, loadFromXML, loadFromXML, loadFromXML, loadFromXML
      • Methods inherited from class org.fife.ui.autocomplete.AbstractCompletionProvider

        addCompletion, addCompletions, addWordCompletions, checkProviderAndAdd, clear, getCompletionByInputText, removeCompletion
      • Methods inherited from class org.fife.ui.autocomplete.CompletionProviderBase

        clearParameterizedCompletionParams, getCompletions, getListCellRenderer, getParameterChoicesProvider, getParameterListEnd, getParameterListSeparator, getParameterListStart, getParent, isAutoActivateOkay, setAutoActivationRules, setListCellRenderer, setParameterChoicesProvider, setParameterizedCompletionParams, setParent
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SourceCompletionProvider

        SourceCompletionProvider()
        Constructor.
      • SourceCompletionProvider

        SourceCompletionProvider​(JarManager jarManager)
        Constructor.
        Parameters:
        jarManager - The jar manager for this provider.
    • Method Detail

      • addCompletionsForStaticMembers

        private void addCompletionsForStaticMembers​(java.util.Set<org.fife.ui.autocomplete.Completion> set,
                                                    CompilationUnit cu,
                                                    ClassFile cf,
                                                    java.lang.String pkg)
      • addCompletionsForExtendedClass

        private void addCompletionsForExtendedClass​(java.util.Set<org.fife.ui.autocomplete.Completion> set,
                                                    CompilationUnit cu,
                                                    ClassFile cf,
                                                    java.lang.String pkg,
                                                    java.util.Map<java.lang.String,​java.lang.String> typeParamMap)
        Adds completions for accessible methods and fields of super classes. This is only called when the caret is inside of a class. TODO: Handle accessibility correctly!
        Parameters:
        set - The set of completions to add to.
        cu - The compilation unit.
        cf - A class in the chain of classes that a type being parsed inherits from.
        pkg - The package of the source being parsed.
        typeParamMap - A mapping of type parameters to type arguments for the object whose fields/methods/etc. are currently being code-completed.
      • addCompletionsForLocalVarsMethods

        private void addCompletionsForLocalVarsMethods​(CompilationUnit cu,
                                                       LocalVariable var,
                                                       java.util.Set<org.fife.ui.autocomplete.Completion> retVal)
        Adds completions for all methods and public fields of a local variable. This will add nothing if the local variable is a primitive type.
        Parameters:
        cu - The compilation unit being parsed.
        var - The local variable.
        retVal - The set to add completions to.
      • addShorthandCompletions

        private void addShorthandCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set)
        Adds simple shorthand completions relevant to Java.
        Parameters:
        set - The set to add to.
      • setShorthandCache

        public void setShorthandCache​(ShorthandCompletionCache shorthandCache)
        Set template completion cache for source completion provider.
        Parameters:
        shorthandCache - The new cache.
      • getClassFileFor

        private ClassFile getClassFileFor​(CompilationUnit cu,
                                          java.lang.String className)
        Gets the ClassFile for a class.
        Parameters:
        cu - The compilation unit being parsed.
        className - The name of the class (fully qualified or not).
        Returns:
        The ClassFile for the class, or null if cf represents java.lang.Object (or if the super class could not be determined).
      • addLocalVarCompletions

        private void addLocalVarCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set,
                                            Method method,
                                            int offs)
        Adds completions for local variables in a method.
        Parameters:
        set - The set of completions to add to.
        method - The method being examined.
        offs - The caret's offset into the source. This should be inside of method.
      • addLocalVarCompletions

        private void addLocalVarCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set,
                                            CodeBlock block,
                                            int offs)
        Adds completions for local variables in a code block inside a method.
        Parameters:
        set - The set of completions to add to.
        block - The code block.
        offs - The caret's offset into the source. This should be inside of block.
      • addJar

        public void addJar​(LibraryInfo info)
                    throws java.io.IOException
        Adds a jar to read from.
        Parameters:
        info - The jar to add. If this is null, then the current JVM's main JRE jar (rt.jar, or classes.jar on OS X) will be added. If this jar has already been added, adding it again will do nothing (except possibly update its attached source location).
        Throws:
        java.io.IOException - If an IO error occurs.
        See Also:
        getJars(), removeJar(File)
      • checkStringLiteralMember

        private boolean checkStringLiteralMember​(javax.swing.text.JTextComponent comp,
                                                 java.lang.String alreadyEntered,
                                                 CompilationUnit cu,
                                                 java.util.Set<org.fife.ui.autocomplete.Completion> set)
        Checks whether the user is typing a completion for a String member after a String literal.
        Parameters:
        comp - The text component.
        alreadyEntered - The text already entered.
        cu - The compilation unit being parsed.
        set - The set to add possible completions to.
        Returns:
        Whether the user is indeed typing a completion for a String literal member.
      • createTypeParamMap

        private java.util.Map<java.lang.String,​java.lang.String> createTypeParamMap​(Type type,
                                                                                          ClassFile cf)
        Creates and returns a mapping of type parameters to type arguments.
        Parameters:
        type - The type of a variable/field/etc. whose fields/methods/etc. are being code completed, as declared in the source. This includes type arguments.
        cf - The ClassFile representing the actual type of the variable/field/etc. being code completed
        Returns:
        A mapping of type parameter names to type arguments (both Strings).
      • getCompletionsAt

        public java.util.List<org.fife.ui.autocomplete.Completion> getCompletionsAt​(javax.swing.text.JTextComponent tc,
                                                                                    java.awt.Point p)
        Specified by:
        getCompletionsAt in interface org.fife.ui.autocomplete.CompletionProvider
        Overrides:
        getCompletionsAt in class org.fife.ui.autocomplete.DefaultCompletionProvider
      • getCompletionsImpl

        protected java.util.List<org.fife.ui.autocomplete.Completion> getCompletionsImpl​(javax.swing.text.JTextComponent comp)
        Overrides:
        getCompletionsImpl in class org.fife.ui.autocomplete.AbstractCompletionProvider
      • getJars

        public java.util.List<LibraryInfo> getJars()
        Returns the jars on the "build path".
        Returns:
        A list of LibraryInfos. Modifying a LibraryInfo in this list will have no effect on this completion provider; in order to do that, you must re-add the jar via addJar(LibraryInfo). If there are no jars on the "build path," this will be an empty list.
        See Also:
        addJar(LibraryInfo)
      • getSourceLocForClass

        public SourceLocation getSourceLocForClass​(java.lang.String className)
      • isAccessible

        private boolean isAccessible​(MemberInfo info,
                                     java.lang.String pkg)
        Returns whether a method defined by a super class is accessible to this class.
        Parameters:
        info - Information about the member.
        pkg - The package of the source currently being parsed.
        Returns:
        Whether the method is accessible.
      • isValidChar

        protected boolean isValidChar​(char ch)
        Overrides:
        isValidChar in class org.fife.ui.autocomplete.DefaultCompletionProvider
      • loadCompletionsForCaretPosition

        private void loadCompletionsForCaretPosition​(CompilationUnit cu,
                                                     javax.swing.text.JTextComponent comp,
                                                     java.lang.String alreadyEntered,
                                                     java.util.Set<org.fife.ui.autocomplete.Completion> retVal)
        Loads completions based on the current caret location in the source. In other words:
        • If the caret is anywhere in a class, the names of all methods and fields in the class are loaded. Methods and fields in super classes are also loaded. TODO: Get super methods/fields added correctly by access!
        • If the caret is in a field, local variables currently accessible are loaded.
        Parameters:
        cu - The compilation unit being parsed.
        comp - The text component.
        alreadyEntered - The already-entered text.
        retVal - The set of values to add to.
      • loadCompletionsForCaretPosition

        private void loadCompletionsForCaretPosition​(CompilationUnit cu,
                                                     javax.swing.text.JTextComponent comp,
                                                     java.lang.String alreadyEntered,
                                                     java.util.Set<org.fife.ui.autocomplete.Completion> retVal,
                                                     TypeDeclaration td,
                                                     java.lang.String prefix,
                                                     int caret)
        Loads completions based on the current caret location in the source. This method is called when the caret is found to be in a specific type declaration. This method checks if the caret is in a child type declaration first, then adds completions for itself next.
        • If the caret is anywhere in a class, the names of all methods and fields in the class are loaded. Methods and fields in super classes are also loaded. TODO: Get super methods/fields added correctly by access!
        • If the caret is in a field, local variables currently accessible are loaded.
        Parameters:
        cu - The compilation unit.
        comp - The text component being analyzed.
        alreadyEntered - The already-entered text.
        retVal - The set of returned completions.
        td - The type declaration.
        prefix - The prefix.
        caret - The caret position.
      • loadCompletionsForCaretPositionQualified

        private void loadCompletionsForCaretPositionQualified​(CompilationUnit cu,
                                                              java.lang.String alreadyEntered,
                                                              java.util.Set<org.fife.ui.autocomplete.Completion> retVal,
                                                              TypeDeclaration td,
                                                              Method currentMethod,
                                                              java.lang.String prefix,
                                                              int offs)
        Loads completions for the text at the current caret position, if there is a "prefix" of chars and at least one '.' character in the text up to the caret. This is currently very limited and needs to be improved.
        Parameters:
        cu - The compilation unit being examined.
        alreadyEntered - The already-entered text.
        retVal - The return value.
        td - The type declaration the caret is in.
        currentMethod - The method the caret is in, or null if none.
        prefix - The text up to the current caret position. This is guaranteed to be non-null not equal to "this".
        offs - The offset of the caret in the document.
      • loadCompletionsForCaretPositionQualifiedCodeBlock

        private void loadCompletionsForCaretPositionQualifiedCodeBlock​(CompilationUnit cu,
                                                                       java.util.Set<org.fife.ui.autocomplete.Completion> retVal,
                                                                       TypeDeclaration td,
                                                                       CodeBlock block,
                                                                       java.lang.String prefix,
                                                                       int offs)
      • loadCompletionsForImport

        private void loadCompletionsForImport​(java.util.Set<org.fife.ui.autocomplete.Completion> set,
                                              java.lang.String importStr,
                                              java.lang.String pkgName)
        Loads completions for a single import statement.
        Parameters:
        importStr - The import statement.
        pkgName - The package of the source currently being parsed.
      • loadImportCompletions

        private void loadImportCompletions​(java.util.Set<org.fife.ui.autocomplete.Completion> set,
                                           java.lang.String text,
                                           CompilationUnit cu)
        Loads completions for all import statements.
        Parameters:
        cu - The compilation unit being parsed.
      • removeJar

        public boolean removeJar​(java.io.File jar)
        Removes a jar from the "build path".
        Parameters:
        jar - The jar to remove.
        Returns:
        Whether the jar was removed. This will be false if the jar was not on the build path.
        See Also:
        addJar(LibraryInfo), getJars(), clearJars()
      • setJavaProvider

        void setJavaProvider​(JavaCompletionProvider javaProvider)
        Sets the parent Java provider.
        Parameters:
        javaProvider - The parent completion provider.