Class CompilationRoot


  • public class CompilationRoot
    extends java.lang.Object
    Encapsulates a collection of script files that should be compiled together as a unit. Files in a compilation unit can reference each other (for example, by importing each other).
    Author:
    Chris Jennings
    • Constructor Summary

      Constructors 
      Constructor Description
      CompilationRoot()
      Creates a new, empty compilation root.
      CompilationRoot​(TSLanguageServices ts)
      Creates a new, empty compilation root that uses the specified language services instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(SourceUnit source)
      Adds a source file to the compilation root.
      SourceUnit add​(java.lang.String identifier, java.lang.String text)
      A convenience method that adds or updates a source file directly from an identifier and script text.
      CompiledSource compile​(java.lang.String fileName)
      Compiles a source unit.
      protected SourceUnit createSourceUnitForReferencedFile​(java.lang.String path)
      Gives the compilation root the opportunity to create a source unit for a referenced file that is not already in the root.
      boolean directoryExists​(java.lang.String identifier)
      Returns true if the specified identifier maps to a real object in the the file system and that object is a directory.
      boolean exists​(java.lang.String identifier)
      Returns true if the specified identifier exists.
      SourceUnit get​(java.lang.String fileName)
      Returns the source unit for the specified identifier, or null if there is no such source unit.
      CompletionInfo.EntryDetails getCodeCompletionDetails​(java.lang.String fileName, int position, CompletionInfo.Entry completion)
      Returns additional details about a particular code completion.
      CompletionInfo getCodeCompletions​(java.lang.String fileName, int position)
      Returns code completions for the specified position.
      java.util.List<Diagnostic> getDiagnostics​(java.lang.String fileName, boolean includeSyntactic, boolean includeSemantic)
      Returns a list of diagnostic messages for a source unit.
      void getDiagnostics​(java.lang.String fileName, boolean includeSyntactic, boolean includeSemantic, java.util.function.Consumer<java.util.List<Diagnostic>> callback)
      Gathers a list of diagnostic messages in the background, passing them to the specified callback once they are available.
      NavigationTree getNavigationTree​(java.lang.String fileName)
      Returns a file's current navigation tree.
      void getNavigationTree​(java.lang.String fileName, java.util.function.Consumer<NavigationTree> callback)
      Returns a file's current navigation tree.
      Overview getOverview​(java.lang.String fileName, int position)
      Returns a quick overview at the node at the current position, or null.
      java.lang.Object getSnapshot​(java.lang.String fileName)
      Returns a current script snapshot for the specified file.
      java.lang.String getSuggestedIdentifier​(java.io.File file)
      Suggest an identifier for a file to be added to this compilation root.
      java.lang.String getVersion​(java.lang.String fileName)
      Returns a version tag for the specified file.
      java.lang.String[] list()
      Returns an array of the identifiers for the added sources.
      void remove​(SourceUnit source)
      Removes a source unit.
      void setRootFile​(java.io.File file)
      Set a base folder to create suggested identifiers relative to.
      • Methods inherited from class java.lang.Object

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

      • CompilationRoot

        public CompilationRoot()
        Creates a new, empty compilation root.
      • CompilationRoot

        public CompilationRoot​(TSLanguageServices ts)
        Creates a new, empty compilation root that uses the specified language services instance.
        Parameters:
        ts - the TS services instance, or null to use the default shared instance
    • Method Detail

      • add

        public void add​(SourceUnit source)
        Adds a source file to the compilation root. If an existing source has the same
        Parameters:
        source - the source file to add
      • add

        public SourceUnit add​(java.lang.String identifier,
                              java.lang.String text)
        A convenience method that adds or updates a source file directly from an identifier and script text. If a source unit exists for the identifier, it will be updated. Otherwise a source unit will be created for the identifier using the specified text.
        Parameters:
        identifier - the identifier, such as a path, that uniquely identifies the script
        text - the content of the script
        Returns:
        the source unit that was created for the script
      • remove

        public void remove​(SourceUnit source)
        Removes a source unit.
        Parameters:
        source - the unit to remove
      • get

        public SourceUnit get​(java.lang.String fileName)
        Returns the source unit for the specified identifier, or null if there is no such source unit.
        Parameters:
        fileName - the identifier
        Returns:
        the source unit for the identifier
      • list

        public java.lang.String[] list()
        Returns an array of the identifiers for the added sources.
        Returns:
        all identifiers that are part of this compilation root
      • compile

        public CompiledSource compile​(java.lang.String fileName)
        Compiles a source unit.
        Parameters:
        fileName - the name of the file to compile
        Returns:
        the compilation output
      • getDiagnostics

        public java.util.List<Diagnostic> getDiagnostics​(java.lang.String fileName,
                                                         boolean includeSyntactic,
                                                         boolean includeSemantic)
        Returns a list of diagnostic messages for a source unit.
        Parameters:
        fileName - the file name to get diagnostics for
        includeSyntactic - if true, includes syntax-related diagnostics
        includeSemantic - if true, includes semantic diagnostics
        Returns:
        a list of diagnostics, possibly empty
      • getDiagnostics

        public void getDiagnostics​(java.lang.String fileName,
                                   boolean includeSyntactic,
                                   boolean includeSemantic,
                                   java.util.function.Consumer<java.util.List<Diagnostic>> callback)
        Gathers a list of diagnostic messages in the background, passing them to the specified callback once they are available.
        Parameters:
        fileName - the file name to get diagnostics for
        includeSyntactic - if true, includes syntax-related diagnostics
        includeSemantic - if true, includes semantic diagnostics
        callback - the callback to invoke with the result
      • getCodeCompletions

        public CompletionInfo getCodeCompletions​(java.lang.String fileName,
                                                 int position)
        Returns code completions for the specified position.
        Parameters:
        fileName - the file name to get completions for
        position - the position within the file at which completions should be performed
        Returns:
        possible code completions, or null if no completions are available
      • getCodeCompletionDetails

        public CompletionInfo.EntryDetails getCodeCompletionDetails​(java.lang.String fileName,
                                                                    int position,
                                                                    CompletionInfo.Entry completion)
        Returns additional details about a particular code completion.
        Parameters:
        fileName - the file name that the completion was generated for
        position - the position that the completion was generated for
        completion - the completion to get details for
        Returns:
        additional details
      • getNavigationTree

        public NavigationTree getNavigationTree​(java.lang.String fileName)
        Returns a file's current navigation tree.
        Parameters:
        languageService - the language service that manages the file
        fileName - the file name to get diagnostics for
        Returns:
        the file's current navigation tree
      • getNavigationTree

        public void getNavigationTree​(java.lang.String fileName,
                                      java.util.function.Consumer<NavigationTree> callback)
        Returns a file's current navigation tree.
        Parameters:
        languageService - the language service that manages the file
        fileName - the file name to get diagnostics for
        callback - the callback to invoke with the result
      • getOverview

        public Overview getOverview​(java.lang.String fileName,
                                    int position)
        Returns a quick overview at the node at the current position, or null.
        Parameters:
        fileName - the file name that the completion was generated for
        position - the position that the completion was generated for
        Returns:
        information about the node at the position, or null
      • getVersion

        public java.lang.String getVersion​(java.lang.String fileName)
        Returns a version tag for the specified file.
        Parameters:
        fileName - the script identifier
        Returns:
        the version tag
      • getSnapshot

        public java.lang.Object getSnapshot​(java.lang.String fileName)
        Returns a current script snapshot for the specified file.
        Parameters:
        fileName - the script identifier
        Returns:
        a snapshot object, or null if the root does not contain the specified file
      • setRootFile

        public void setRootFile​(java.io.File file)
        Set a base folder to create suggested identifiers relative to. This can optionally be used by file-based compilation roots to create identifiers using getSuggestedIdentifier(java.io.File).
        Parameters:
        file - the root file for composing identifiers, or null
      • getSuggestedIdentifier

        public java.lang.String getSuggestedIdentifier​(java.io.File file)
        Suggest an identifier for a file to be added to this compilation root. The base class will make the file relative to the root file, if any, and normalize the directory separator to '/'. Subclasses may override this to implement other naming schemes.
        Parameters:
        file - the file to suggest an identifier for
        Returns:
        the suggested identifier
      • exists

        public boolean exists​(java.lang.String identifier)
        Returns true if the specified identifier exists. If the identifier is not already added to this root but a foot file has been set, the base class will test if the identifier exists relative to the root file. If the file exists, it will be added as as a new source unit if allowed.
        Parameters:
        identifier - the identifier, typically a path relative to this root
        Returns:
        true if the file exists
      • directoryExists

        public boolean directoryExists​(java.lang.String identifier)
        Returns true if the specified identifier maps to a real object in the the file system and that object is a directory. If there is a root file, then the identifier must be a directory relative to the root file. If there is no root file, then some identifier must have this identifier as a parent path.
        Parameters:
        identifier - the identifier to check
        Returns:
        true if it exists and is a directory
      • createSourceUnitForReferencedFile

        protected SourceUnit createSourceUnitForReferencedFile​(java.lang.String path)
        Gives the compilation root the opportunity to create a source unit for a referenced file that is not already in the root. May return a suitable source unit for the file, or null to indicate no source is available.

        The base class will check if a root file has been set, in which case it will return an EditableSourceUnit for the path if such a file exists relative to the root.

        Parameters:
        path - the compilation root path (path realative to root file)
        Returns:
        the source unit, or null