-
Notifications
You must be signed in to change notification settings - Fork 0
Add typescript-language-server support #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
observerw
merged 7 commits into
main
from
copilot/add-typescript-language-server-support
Dec 18, 2025
+159
−6
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e32402
Initial plan
Copilot 01301e1
Add support for typescript-language-server
Copilot b95c3fd
Remove unused attributes from TsserverClient
Copilot cef6612
Remove WithRequestCallHierarchy capability from TsserverClient
Copilot 061a1eb
Improve error message and add missing Position import to example
Copilot bc1f3d6
Rename TsserverClient to TypescriptClient and TsserverLocalServer to …
Copilot a5c96d1
Add documentation references for initialization options and TypeScrip…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Example: Using TypeScript Language Server for TypeScript code analysis | ||
| # | ||
| # This example demonstrates how to use the TypeScript Language Server | ||
| # to find definition locations in TypeScript code. The typescript-language-server | ||
| # provides powerful static analysis for TypeScript and JavaScript projects. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import anyio | ||
|
|
||
| from lsp_client import Position # noqa: F401 - Used in commented example code | ||
| from lsp_client.clients.typescript import TypescriptClient, TypescriptServer | ||
|
|
||
|
|
||
| async def main(): | ||
| # Set up workspace directory | ||
| workspace = Path.cwd() | ||
| async with TypescriptClient( | ||
| server=TypescriptServer(), | ||
| workspace=workspace, | ||
| ) as client: | ||
| # Example: Find definition of TypescriptClient at line 13, column 28 | ||
| # This demonstrates the definition lookup capability | ||
| print(f"Using language server for: {client.get_language_id()}") | ||
|
|
||
| # In a real scenario, you would provide a TypeScript file path | ||
| # refs = await client.request_definition_locations( | ||
| # file_path="src/index.ts", | ||
| # position=Position(10, 15), | ||
| # ) | ||
| # | ||
| # if not refs: | ||
| # print("No definition locations found.") | ||
| # return | ||
| # | ||
| # # Display all definition locations found | ||
| # for ref in refs: | ||
| # print(f"Definition location found at {ref.uri} - Range: {ref.range}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| anyio.run(main) # Run the async example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import shutil | ||
| from functools import partial | ||
| from subprocess import CalledProcessError | ||
| from typing import Any, override | ||
|
|
||
| import anyio | ||
| from attrs import define | ||
| from loguru import logger | ||
|
|
||
| from lsp_client.capability.request import ( | ||
| WithRequestDefinition, | ||
| WithRequestDocumentSymbol, | ||
| WithRequestHover, | ||
| WithRequestImplementation, | ||
| WithRequestReferences, | ||
| WithRequestTypeDefinition, | ||
| WithRequestWorkspaceSymbol, | ||
| ) | ||
| from lsp_client.capability.server_notification import ( | ||
| WithReceivePublishDiagnostics, | ||
| ) | ||
| from lsp_client.capability.server_notification.log_message import WithReceiveLogMessage | ||
| from lsp_client.client.abc import LSPClient | ||
| from lsp_client.server.docker import DockerServer | ||
| from lsp_client.server.local import LocalServer | ||
| from lsp_client.utils.types import lsp_type | ||
|
|
||
| TypescriptServer = partial( | ||
| LocalServer, command=["typescript-language-server", "--stdio"] | ||
| ) | ||
| TypescriptDockerServer = partial(DockerServer, image="docker.io/lspcontainers/tsserver") | ||
|
|
||
|
|
||
| @define | ||
| class TypescriptClient( | ||
| LSPClient, | ||
| WithRequestHover, | ||
| WithRequestDefinition, | ||
| WithRequestReferences, | ||
| WithRequestImplementation, | ||
| WithRequestTypeDefinition, | ||
| WithRequestDocumentSymbol, | ||
| WithRequestWorkspaceSymbol, | ||
| WithReceiveLogMessage, | ||
| WithReceivePublishDiagnostics, | ||
| ): | ||
| """ | ||
| - Language: TypeScript, JavaScript | ||
| - Homepage: https://github.com/typescript-language-server/typescript-language-server | ||
| - Doc: https://github.com/typescript-language-server/typescript-language-server#readme | ||
| - Github: https://github.com/typescript-language-server/typescript-language-server | ||
| - VSCode Extension: Built-in TypeScript support in VS Code | ||
| """ | ||
|
|
||
| # Preferences for TypeScript/JavaScript language features | ||
| # Reference: https://github.com/typescript-language-server/typescript-language-server#initializationoptions | ||
| suggest_complete_function_calls: bool = True | ||
| include_automatic_optional_chain_completions: bool = True | ||
| include_completions_for_module_exports: bool = True | ||
| include_completions_with_insert_text: bool = True | ||
|
|
||
| @override | ||
| def get_language_id(self) -> lsp_type.LanguageKind: | ||
| return lsp_type.LanguageKind.TypeScript | ||
|
|
||
| @override | ||
| def create_initialization_options(self) -> dict[str, Any]: | ||
| return { | ||
| "preferences": { | ||
| "includeCompletionsForModuleExports": self.include_completions_for_module_exports, | ||
| "includeAutomaticOptionalChainCompletions": self.include_automatic_optional_chain_completions, | ||
| "includeCompletionsWithInsertText": self.include_completions_with_insert_text, | ||
| }, | ||
| "suggest": { | ||
| "completeFunctionCalls": self.suggest_complete_function_calls, | ||
| }, | ||
| } | ||
|
|
||
| @override | ||
| def check_server_compatibility(self, info: lsp_type.ServerInfo | None) -> None: | ||
| return | ||
|
|
||
| @override | ||
| async def ensure_installed(self) -> None: | ||
| if shutil.which("typescript-language-server"): | ||
| return | ||
|
|
||
| logger.warning( | ||
| "typescript-language-server not found, attempting to install via npm..." | ||
| ) | ||
|
|
||
| try: | ||
| # typescript-language-server requires the TypeScript compiler as a peer dependency | ||
| # Reference: https://github.com/typescript-language-server/typescript-language-server#installing | ||
| await anyio.run_process( | ||
| ["npm", "install", "-g", "typescript-language-server", "typescript"] | ||
observerw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| logger.info("Successfully installed typescript-language-server via npm") | ||
| return | ||
| except CalledProcessError as e: | ||
| raise RuntimeError( | ||
| "Could not install typescript-language-server and typescript. Please install them manually with 'npm install -g typescript-language-server typescript'. " | ||
| "See https://github.com/typescript-language-server/typescript-language-server for more information." | ||
| ) from e | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.