Class SourceCompletionProvider

java.lang.Object
org.fife.ui.autocomplete.CompletionProviderBase
org.fife.ui.autocomplete.AbstractCompletionProvider
org.fife.ui.autocomplete.DefaultCompletionProvider
org.fife.rsta.ac.java.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
  • Field Details

  • Constructor Details

    • SourceCompletionProvider

      SourceCompletionProvider()
      Constructor.
    • SourceCompletionProvider

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

    • addCompletionsForStaticMembers

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

      private void addCompletionsForExtendedClass(Set<org.fife.ui.autocomplete.Completion> set, CompilationUnit cu, ClassFile cf, String pkg, Map<String,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, 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(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, 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(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(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 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:
      IOException - If an IO error occurs.
      See Also:
    • checkStringLiteralMember

      private boolean checkStringLiteralMember(JTextComponent comp, String alreadyEntered, CompilationUnit cu, 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.
    • clearJars

      public void clearJars()
      Removes all jars from the "build path".
      See Also:
    • createTypeParamMap

      private Map<String,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 List<org.fife.ui.autocomplete.Completion> getCompletionsAt(JTextComponent tc, Point p)
      Specified by:
      getCompletionsAt in interface org.fife.ui.autocomplete.CompletionProvider
      Overrides:
      getCompletionsAt in class org.fife.ui.autocomplete.DefaultCompletionProvider
    • getCompletionsImpl

      protected List<org.fife.ui.autocomplete.Completion> getCompletionsImpl(JTextComponent comp)
      Overrides:
      getCompletionsImpl in class org.fife.ui.autocomplete.AbstractCompletionProvider
    • getJars

      public 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:
    • getSourceLocForClass

      public SourceLocation getSourceLocForClass(String className)
    • isAccessible

      private boolean isAccessible(MemberInfo info, 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, JTextComponent comp, String alreadyEntered, 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, JTextComponent comp, String alreadyEntered, Set<org.fife.ui.autocomplete.Completion> retVal, TypeDeclaration td, 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, String alreadyEntered, Set<org.fife.ui.autocomplete.Completion> retVal, TypeDeclaration td, Method currentMethod, 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, Set<org.fife.ui.autocomplete.Completion> retVal, TypeDeclaration td, CodeBlock block, String prefix, int offs)
    • loadCompletionsForImport

      private void loadCompletionsForImport(Set<org.fife.ui.autocomplete.Completion> set, String importStr, 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(Set<org.fife.ui.autocomplete.Completion> set, String text, CompilationUnit cu)
      Loads completions for all import statements.
      Parameters:
      cu - The compilation unit being parsed.
    • removeJar

      public boolean removeJar(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:
    • setJavaProvider

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