Class WorkspaceIndexPlugin

java.lang.Object
org.eclipse.lemminx.extensions.index.WorkspaceIndexPlugin
All Implemented Interfaces:
IWorkspaceServiceParticipant, IXMLExtension

public class WorkspaceIndexPlugin extends Object implements IXMLExtension, IWorkspaceServiceParticipant
LSP extension that owns the lifecycle of the workspace Bloom index.

Lifecycle:

   initialize  → start()                 — register participants, read initial settings
   initialized → initialized()           — trigger async workspace scan (if enabled)
   didSave / settings change → doSave()  — re-index open file or react to setting change
   file watcher → didChangeWatchedFiles()— incremental index update (Created/Changed/Deleted)
   willRenameFiles               — compute WorkspaceEdit for grammar references
   shutdown → stop()                     — unregister participants, clear index
 

Enabled state: The index is disabled by default (enabled = false). It becomes active only when the client sends "xml.workspaceIndex.enabled": true in initializationOptions or via a subsequent didChangeConfiguration. This avoids the startup scan cost for users who have not opted in.

One-read-per-file principle: Each file is read at most once per indexing event. The resulting FileBloomEntry stores both the probabilistic BloomFilter (for O(1) candidate lookup) and the confirmed GrammarReference list (for zero-I/O code lens counts). Code lens refresh and Find References never re-read files from disk.

Loaded via ServiceLoader from META-INF/services/org.eclipse.lemminx.services.extensions.IXMLExtension.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    didChangeWatchedFiles(List<org.eclipse.lsp4j.FileEvent> changes, IWatchedFilesContext context)
    Keeps the index in sync with file system changes reported by the client.
    void
    didChangeWorkspaceFolders(org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams params)
    Updates the tracked folder list and triggers an incremental scan for newly added folders.
    void
    Reacts to document saves and workspace settings changes.
    void
    initialized(org.eclipse.lsp4j.InitializedParams params, XMLExtensionsRegistry registry)
    Called when the client sends the initialized notification, signalling that it is ready to receive requests.
    static String
    Returns the in-memory content of the given URI if it is currently open in the editor, or null if the file is not open.
    void
    start(org.eclipse.lsp4j.InitializeParams params, XMLExtensionsRegistry registry)
    Called once by the extension registry when the LSP initialize request is received.
    void
    Unregisters all participants and clears the index on server shutdown.
    org.eclipse.lsp4j.WorkspaceEdit
    willRenameFiles(org.eclipse.lsp4j.RenameFilesParams params, org.eclipse.lsp4j.jsonrpc.CancelChecker cancelChecker)
    Computes text edits to keep grammar references consistent when files are renamed.

    Methods inherited from class java.lang.Object

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

    • WorkspaceIndexPlugin

      public WorkspaceIndexPlugin()
  • Method Details

    • start

      public void start(org.eclipse.lsp4j.InitializeParams params, XMLExtensionsRegistry registry)
      Called once by the extension registry when the LSP initialize request is received. Registers the index participants and reads the initial xml.workspaceIndex.enabled setting from initializationOptions.

      Use case: VS Code starts with the LemMinX server and sends initialize with "xml.workspaceIndex.enabled": true. This method registers the reference and code-lens participants so they are active from the first request, and stores the workspace folders for the upcoming scan.

      Specified by:
      start in interface IXMLExtension
      Parameters:
      params -
      registry -
    • initialized

      public void initialized(org.eclipse.lsp4j.InitializedParams params, XMLExtensionsRegistry registry)
      Called when the client sends the initialized notification, signalling that it is ready to receive requests. Triggers the async workspace scan.

      Use case: After VS Code finishes loading LemMinX, it sends initialized. The plugin sets clientInitialized = true and, if the index is enabled, spawns a background thread that walks the workspace folders and builds Bloom filters. Once the scan completes, it sends workspace/codeLens/refresh so grammar files show up-to-date counts.

      Specified by:
      initialized in interface IXMLExtension
      Parameters:
      params - the initialized notification params (usually empty)
      registry - the extensions registry
    • doSave

      public void doSave(ISaveContext context)
      Reacts to document saves and workspace settings changes.

      Use case — document saved: The user edits order.xml, adds xsi:noNamespaceSchemaLocation="schema.xsd", then saves. The file is re-indexed immediately so subsequent WorkspaceIndex.findReferencingFiles(java.lang.String) calls include it. ICodeLensRefresher.requestCodeLensRefresh() is called only when WorkspaceIndex.indexFile(java.nio.file.Path) returns true — i.e., the file actually contains grammar references. A plain XML with no declarations does not trigger an unnecessary client round-trip.

      Use case — disable: The user sets "xml.workspaceIndex.enabled": false in VS Code settings. The index is cleared immediately so no stale candidates are served.

      Use case — re-enable: The user toggles back to true. If the client is already initialized (initialized was already received), a fresh async scan is triggered and code lenses are refreshed. This avoids showing zero references after re-enabling.

      Use case — catalog settings change: The user adds or removes a catalog path in xml.catalogs. Added catalogs are indexed immediately so grammar files they reference appear in Find References / code lens. Removed catalogs are evicted from the index. A code lens refresh is requested when any catalog changes — reference counts on grammar files may have changed.

      Specified by:
      doSave in interface IXMLExtension
      Parameters:
      context -
    • didChangeWorkspaceFolders

      public void didChangeWorkspaceFolders(org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams params)
      Updates the tracked folder list and triggers an incremental scan for newly added folders.

      Use case: The user adds a second root folder (/home/user/lib) to the VS Code workspace via "Add Folder to Workspace". VS Code sends workspace/didChangeWorkspaceFolders with added=[lib]. This method appends lib to the tracked folders and launches an async scan covering only the added folder so the existing index is not rebuilt. Deleted folders have their files evicted by the subsequent file-watcher events.

      Specified by:
      didChangeWorkspaceFolders in interface IWorkspaceServiceParticipant
      Parameters:
      params - the workspace folders change description
      See Also:
      • WorkspaceService.didChangeWorkspaceFolders(DidChangeWorkspaceFoldersParams)
    • didChangeWatchedFiles

      public void didChangeWatchedFiles(List<org.eclipse.lsp4j.FileEvent> changes, IWatchedFilesContext context)
      Keeps the index in sync with file system changes reported by the client.

      Use case — file copied / created: The user copies or creates an XML file shipping.xml that contains xsi:noNamespaceSchemaLocation="schema.xsd". The file watcher fires a Created event; the plugin re-indexes the file so it appears in future WorkspaceIndex.findReferencingFiles(java.lang.String) calls. ICodeLensRefresher.requestCodeLensRefresh() is called only when WorkspaceIndex.indexFile(java.nio.file.Path) returns true — i.e., the file contains at least one grammar reference. Creating a plain XML file with no declarations does not trigger an unnecessary client round-trip. The registry sends exactly one workspace/codeLens/refresh after all participants finish, regardless of how many files triggered a refresh request (same N-to-1 pattern as ICodeLensRefresher.requestCodeLensRefresh()).

      Use case — file deleted: The user deletes old.xml. The plugin removes its Bloom entry even when the index is disabled, so stale entries do not accumulate.

      Specified by:
      didChangeWatchedFiles in interface IWorkspaceServiceParticipant
      Parameters:
      changes - the list of file events.
      context - context for requesting deferred actions (e.g. code lens refresh).
      See Also:
      • WorkspaceService.didChangeWatchedFiles(DidChangeWatchedFilesParams)
    • willRenameFiles

      public org.eclipse.lsp4j.WorkspaceEdit willRenameFiles(org.eclipse.lsp4j.RenameFilesParams params, org.eclipse.lsp4j.jsonrpc.CancelChecker cancelChecker)
      Computes text edits to keep grammar references consistent when files are renamed.

      Use case: The user renames schemas/order.xsd to schemas/order-v2.xsd via the VS Code file explorer. Before the rename is applied, VS Code sends workspace/willRenameFiles. This method delegates to GrammarFileRenameSupport for each renamed file and merges the resulting TextEdits into a single WorkspaceEdit. When multiple grammar files are renamed in one operation, all edits are accumulated into a single map (each XML file appears at most once per key).

      Specified by:
      willRenameFiles in interface IWorkspaceServiceParticipant
      Parameters:
      params - the rename parameters (old/new URIs).
      cancelChecker - checker to abort long-running computations.
      Returns:
      the merged edit, or null if none of the renamed files are referenced by any indexed XML file.
      See Also:
      • WorkspaceService.willRenameFiles(RenameFilesParams)
    • stop

      public void stop(XMLExtensionsRegistry registry)
      Unregisters all participants and clears the index on server shutdown.

      Use case: VS Code sends shutdown then exit. The extension registry calls stop on each extension. This method removes the reference and code-lens participants so no dangling callbacks remain, and clears the Bloom index to release memory before the JVM exits.

      Specified by:
      stop in interface IXMLExtension
      Parameters:
      registry -
    • openDocumentContent

      public static String openDocumentContent(IXMLDocumentProvider docProvider, String uri)
      Returns the in-memory content of the given URI if it is currently open in the editor, or null if the file is not open.

      Direct URI lookup is tried first. If it fails (LSP clients differ in URI format — VS Code sends lowercase drive letters, LSP4E uppercase, and Path.toUri() also produces uppercase on Windows), a path-based fallback iterates all open documents and compares via Path.equals(java.lang.Object), which is case-insensitive on Windows.

      Parameters:
      docProvider - the document provider.
      uri - URI of the candidate file (from the workspace index).
      Returns:
      the live document text, or null if the file is not open.