Class WorkspaceIndexPlugin
- All Implemented Interfaces:
IWorkspaceServiceParticipant,IXMLExtension
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 -
Method Summary
Modifier and TypeMethodDescriptionvoiddidChangeWatchedFiles(List<org.eclipse.lsp4j.FileEvent> changes, IWatchedFilesContext context) Keeps the index in sync with file system changes reported by the client.voiddidChangeWorkspaceFolders(org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams params) Updates the tracked folder list and triggers an incremental scan for newly added folders.voiddoSave(ISaveContext context) Reacts to document saves and workspace settings changes.voidinitialized(org.eclipse.lsp4j.InitializedParams params, XMLExtensionsRegistry registry) Called when the client sends theinitializednotification, signalling that it is ready to receive requests.static StringopenDocumentContent(IXMLDocumentProvider docProvider, String uri) Returns the in-memory content of the given URI if it is currently open in the editor, ornullif the file is not open.voidstart(org.eclipse.lsp4j.InitializeParams params, XMLExtensionsRegistry registry) Called once by the extension registry when the LSPinitializerequest is received.voidstop(XMLExtensionsRegistry registry) Unregisters all participants and clears the index on server shutdown.org.eclipse.lsp4j.WorkspaceEditwillRenameFiles(org.eclipse.lsp4j.RenameFilesParams params, org.eclipse.lsp4j.jsonrpc.CancelChecker cancelChecker) Computes text edits to keep grammar references consistent when files are renamed.
-
Constructor Details
-
WorkspaceIndexPlugin
public WorkspaceIndexPlugin()
-
-
Method Details
-
start
Called once by the extension registry when the LSPinitializerequest is received. Registers the index participants and reads the initialxml.workspaceIndex.enabledsetting frominitializationOptions.Use case: VS Code starts with the LemMinX server and sends
initializewith"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:
startin interfaceIXMLExtension- Parameters:
params-registry-
-
initialized
Called when the client sends theinitializednotification, 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 setsclientInitialized = trueand, if the index is enabled, spawns a background thread that walks the workspace folders and builds Bloom filters. Once the scan completes, it sendsworkspace/codeLens/refreshso grammar files show up-to-date counts.- Specified by:
initializedin interfaceIXMLExtension- Parameters:
params- the initialized notification params (usually empty)registry- the extensions registry
-
doSave
Reacts to document saves and workspace settings changes.Use case — document saved: The user edits
order.xml, addsxsi:noNamespaceSchemaLocation="schema.xsd", then saves. The file is re-indexed immediately so subsequentWorkspaceIndex.findReferencingFiles(java.lang.String)calls include it.ICodeLensRefresher.requestCodeLensRefresh()is called only whenWorkspaceIndex.indexFile(java.nio.file.Path)returnstrue— 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": falsein 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 (initializedwas 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:
doSavein interfaceIXMLExtension- 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 sendsworkspace/didChangeWorkspaceFolderswithadded=[lib]. This method appendslibto 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:
didChangeWorkspaceFoldersin interfaceIWorkspaceServiceParticipant- Parameters:
params- the workspace folders change description- See Also:
-
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.xmlthat containsxsi:noNamespaceSchemaLocation="schema.xsd". The file watcher fires aCreatedevent; the plugin re-indexes the file so it appears in futureWorkspaceIndex.findReferencingFiles(java.lang.String)calls.ICodeLensRefresher.requestCodeLensRefresh()is called only whenWorkspaceIndex.indexFile(java.nio.file.Path)returnstrue— 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 oneworkspace/codeLens/refreshafter all participants finish, regardless of how many files triggered a refresh request (same N-to-1 pattern asICodeLensRefresher.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:
didChangeWatchedFilesin interfaceIWorkspaceServiceParticipant- Parameters:
changes- the list of file events.context- context for requesting deferred actions (e.g. code lens refresh).- See Also:
-
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.xsdtoschemas/order-v2.xsdvia the VS Code file explorer. Before the rename is applied, VS Code sendsworkspace/willRenameFiles. This method delegates toGrammarFileRenameSupportfor each renamed file and merges the resultingTextEdits into a singleWorkspaceEdit. 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:
willRenameFilesin interfaceIWorkspaceServiceParticipant- Parameters:
params- the rename parameters (old/new URIs).cancelChecker- checker to abort long-running computations.- Returns:
- the merged edit, or
nullif none of the renamed files are referenced by any indexed XML file. - See Also:
-
stop
Unregisters all participants and clears the index on server shutdown.Use case: VS Code sends
shutdownthenexit. The extension registry callsstopon 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:
stopin interfaceIXMLExtension- Parameters:
registry-
-
openDocumentContent
Returns the in-memory content of the given URI if it is currently open in the editor, ornullif 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 viaPath.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
nullif the file is not open.
-