Class LanguageService
- All Implemented Interfaces:
AutoCloseable
Lifecycle
- Construct a
LanguageServicewith the server launch command and the workspace root directory. - Call
start()to launch the server process, connect the LSP4J launcher over the process's stdin/stdout, and perform the LSPinitialize/initializedhandshake. - Use the navigation methods.
- Call
close()(or use try-with-resources) to send the LSPshutdown+exitmessages and terminate the server process.
All position parameters are 1-based
The line and character parameters accepted by every
operation use the same 1-based numbering displayed in editors.
The client converts them to the 0-based coordinates required by the LSP
specification transparently.
Thread safety
Each operation method may be called from any thread after start()
returns. Internally, all calls are forwarded over the single LSP4J channel
whose threading is managed by the LSP4J message reader/writer threads.
-
Constructor Summary
ConstructorsConstructorDescriptionLanguageService(List<String> command, Path workspaceRoot) Creates aLanguageServiceusing a pre-parsed command list.LanguageService(List<String> command, Path workspaceRoot, int timeoutSeconds) Creates aLanguageServicewith a custom per-request timeout. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Sends the LSPshutdownrequest followed by theexitnotification, then forcibly terminates the server process if it is still running.documentSymbol(String filePath) Returns all symbols defined in the given document.findReferences(String filePath, int line, int character) Finds all references to the symbol at the given position, including the declaration site.findReferences(String filePath, int line, int character, boolean includeDeclaration) Finds all references to the symbol at the given position.static LanguageServiceReturns the service for the given language.goToDefinition(String filePath, int line, int character) Finds the definition location(s) of the symbol at the given position.goToImplementation(String filePath, int line, int character) Finds all implementations of the interface or abstract method at the given position.Returns hover information (documentation, type info) for the symbol at the given position.incomingCalls(String filePath, int line, int character) Convenience method that combinesprepareCallHierarchy(String, int, int)andincomingCalls(CallHierarchyItem)into a single call.Returns all callers of the given call-hierarchy item.booleanReturns true if the service has been started and initialized.booleanReturnstrueif the client has been successfully started and the initialize handshake has completed.static LanguageServiceCreates aLanguageServicepre-configured to launch the Eclipse JDT Language Server (JDT-LS).static LanguageServiceCreates aLanguageServicefor any language server that accepts a simplestdiolaunch command (e.g.outgoingCalls(String filePath, int line, int character) Convenience method that combinesprepareCallHierarchy(String, int, int)andoutgoingCalls(CallHierarchyItem)into a single call.Returns all functions or methods called by the given call-hierarchy item.prepareCallHierarchy(String filePath, int line, int character) Resolves the call-hierarchy item at the given position.static voidput(String lang, LanguageService service) Registers aLanguageServicefor the given language ID.org.eclipse.lsp4j.services.LanguageServerserver()Returns the underlying language server.voidstart()Launches the language server process, connects the LSP4J launcher over the process's stdin/stdout, and performs the LSPinitialize/initializedhandshake.voidstart(org.eclipse.lsp4j.services.LanguageClient client) Launches the language server process, connects the LSP4J launcher over the process's stdin/stdout, and performs the LSPinitialize/initializedhandshake.Returns the workspace root directory used by this client.workspaceSymbol(String query) Searches for symbols whose names match the given query across the entire workspace.
-
Constructor Details
-
LanguageService
Creates aLanguageServiceusing a pre-parsed command list.- Parameters:
command- the command and arguments used to start the language server process (e.g.["java", "-jar", "jdt-ls.jar", ...]).workspaceRoot- the workspace root directory sent to the server in theinitializerequest.
-
LanguageService
Creates aLanguageServicewith a custom per-request timeout.- Parameters:
command- the command used to start the language server.workspaceRoot- the workspace root directory.timeoutSeconds- how many seconds to wait for each LSP response before throwing aTimeoutException.
-
-
Method Details
-
get
Returns the service for the given language.- Parameters:
lang- the language ID (e.g. "java", "python").- Returns:
- the corresponding
LanguageService, ornullif not found.
-
put
Registers aLanguageServicefor the given language ID.- Parameters:
lang- the language ID (e.g. "java", "python").service- the service to register.
-
server
public org.eclipse.lsp4j.services.LanguageServer server()Returns the underlying language server.- Returns:
- the underlying language server.
-
isInitialized
public boolean isInitialized()Returns true if the service has been started and initialized.- Returns:
- true if the service is initialized, false otherwise.
-
start
Launches the language server process, connects the LSP4J launcher over the process's stdin/stdout, and performs the LSPinitialize/initializedhandshake. The server may send notifications (e.g. publishDiagnostics) back to the client. This overload uses a no-op LSP4J client implementation that log all server notifications at DEBUG or INFO level.This method blocks until the server has acknowledged the
initializerequest.- Throws:
IOException- if the server process cannot be started.InterruptedException- if the thread is interrupted while waiting for the initialize response.ExecutionException- if the initialize request fails.TimeoutException- if the server does not respond within the configured timeout.
-
start
public void start(org.eclipse.lsp4j.services.LanguageClient client) throws IOException, InterruptedException, ExecutionException, TimeoutException Launches the language server process, connects the LSP4J launcher over the process's stdin/stdout, and performs the LSPinitialize/initializedhandshake.This method blocks until the server has acknowledged the
initializerequest.- Parameters:
client- an implementation of LSP4J'sLanguageClientinterface to receive server notifications.- Throws:
IOException- if the server process cannot be started.InterruptedException- if the thread is interrupted while waiting for the initialize response.ExecutionException- if the initialize request fails.TimeoutException- if the server does not respond within the configured timeout.
-
close
public void close()Sends the LSPshutdownrequest followed by theexitnotification, then forcibly terminates the server process if it is still running.- Specified by:
closein interfaceAutoCloseable
-
goToDefinition
Finds the definition location(s) of the symbol at the given position.Corresponds to the LSP
textDocument/definitionrequest.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.- Returns:
- the list of definition locations; empty if none found.
- Throws:
LspException- if the request fails or times out.
-
findReferences
public List<LspLocation> findReferences(String filePath, int line, int character, boolean includeDeclaration) Finds all references to the symbol at the given position.Corresponds to the LSP
textDocument/referencesrequest. The definition location is included in the results whenincludeDeclarationistrue.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.includeDeclaration- whether to include the declaration site.- Returns:
- the list of reference locations; empty if none found.
- Throws:
LspException- if the request fails or times out.
-
findReferences
Finds all references to the symbol at the given position, including the declaration site.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.- Returns:
- the list of reference locations; empty if none found.
- Throws:
LspException- if the request fails or times out.
-
hover
Returns hover information (documentation, type info) for the symbol at the given position.Corresponds to the LSP
textDocument/hoverrequest.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.- Returns:
- the hover text, or an empty string if no hover info is available.
- Throws:
LspException- if the request fails or times out.
-
documentSymbol
Returns all symbols defined in the given document.Corresponds to the LSP
textDocument/documentSymbolrequest.- Parameters:
filePath- the absolute file path to query.- Returns:
- the list of symbols; empty if the server returns nothing.
- Throws:
LspException- if the request fails or times out.
-
workspaceSymbol
Searches for symbols whose names match the given query across the entire workspace.Corresponds to the LSP
workspace/symbolrequest.- Parameters:
query- the search query string (may be empty to list all symbols).- Returns:
- the list of matching symbols; empty if the server returns nothing.
- Throws:
LspException- if the request fails or times out.
-
goToImplementation
Finds all implementations of the interface or abstract method at the given position.Corresponds to the LSP
textDocument/implementationrequest.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.- Returns:
- the list of implementation locations; empty if none found.
- Throws:
LspException- if the request fails or times out.
-
prepareCallHierarchy
Resolves the call-hierarchy item at the given position.This is the first step of a call-hierarchy query. Pass the returned items to
incomingCalls(CallHierarchyItem)oroutgoingCalls(CallHierarchyItem)to traverse the hierarchy.Corresponds to the LSP
textDocument/prepareCallHierarchyrequest.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.- Returns:
- the list of call-hierarchy items at the position; empty if none.
- Throws:
LspException- if the request fails or times out.
-
incomingCalls
Returns all callers of the given call-hierarchy item.Corresponds to the LSP
callHierarchy/incomingCallsrequest.- Parameters:
item- the call-hierarchy item to query (typically obtained fromprepareCallHierarchy(String, int, int)).- Returns:
- the list of incoming calls; empty if none.
- Throws:
LspException- if the request fails or times out.
-
outgoingCalls
Returns all functions or methods called by the given call-hierarchy item.Corresponds to the LSP
callHierarchy/outgoingCallsrequest.- Parameters:
item- the call-hierarchy item to query (typically obtained fromprepareCallHierarchy(String, int, int)).- Returns:
- the list of outgoing calls; empty if none.
- Throws:
LspException- if the request fails or times out.
-
incomingCalls
Convenience method that combinesprepareCallHierarchy(String, int, int)andincomingCalls(CallHierarchyItem)into a single call.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.- Returns:
- the flat list of all incoming calls; empty if none.
- Throws:
LspException- if any request fails or times out.
-
outgoingCalls
Convenience method that combinesprepareCallHierarchy(String, int, int)andoutgoingCalls(CallHierarchyItem)into a single call.- Parameters:
filePath- the absolute file path to query.line- the 1-based line number.character- the 1-based character offset.- Returns:
- the flat list of all outgoing calls; empty if none.
- Throws:
LspException- if any request fails or times out.
-
workspaceRoot
Returns the workspace root directory used by this client.- Returns:
- the workspace root.
-
isStarted
public boolean isStarted()Returnstrueif the client has been successfully started and the initialize handshake has completed.- Returns:
trueif ready.
-
jdtLs
public static LanguageService jdtLs(Path workspaceRoot, Path jdtLsJar, Path jdtLsHome, Path dataDir) Creates aLanguageServicepre-configured to launch the Eclipse JDT Language Server (JDT-LS).The
jdtLsJarpath must point to the JDT-LS launcher JAR (plugins/org.eclipse.equinox.launcher_*.jarinside the JDT-LS installation directory).The data directory is where JDT-LS stores its workspace index. It must be writable and is typically a subdirectory of the user's home directory.
- Parameters:
workspaceRoot- the project root to analyze.jdtLsJar- path to the JDT-LS equinox launcher JAR.jdtLsHome- the JDT-LS installation directory (containsplugins/andconfig_linux/etc.).dataDir- writable directory for JDT-LS workspace data.- Returns:
- a configured (but not yet started)
LanguageService.
-
of
Creates aLanguageServicefor any language server that accepts a simplestdiolaunch command (e.g. Pyright, TypeScript LS, rust-analyzer, clangd).- Parameters:
workspaceRoot- the project root to analyze.serverCommand- the full launch command as a single string (e.g."pyright-langserver --stdio").- Returns:
- a configured (but not yet started)
LanguageService.
-