python server

This commit is contained in:
2025-07-15 22:11:41 +02:00
parent 12ed7ff2bc
commit 9febcebddb
23 changed files with 900 additions and 256 deletions
+39
View File
@@ -0,0 +1,39 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""Debugging support for LSP."""
import os
import pathlib
import runpy
import sys
def update_sys_path(path_to_add: str) -> None:
"""Add given path to `sys.path`."""
if path_to_add not in sys.path and os.path.isdir(path_to_add):
sys.path.append(path_to_add)
# Ensure debugger is loaded before we load anything else, to debug initialization.
debugger_path = os.getenv("DEBUGPY_PATH", None)
if debugger_path:
if debugger_path.endswith("debugpy"):
debugger_path = os.fspath(pathlib.Path(debugger_path).parent)
update_sys_path(debugger_path)
# pylint: disable=wrong-import-position,import-error
import debugpy
# 5678 is the default port, If you need to change it update it here
# and in launch.json.
debugpy.connect(5678)
# This will ensure that execution is paused as soon as the debugger
# connects to VS Code. If you don't want to pause here comment this
# line and set breakpoints as appropriate.
debugpy.breakpoint()
SERVER_PATH = os.fspath(pathlib.Path(__file__).parent / "lsp_server.py")
# NOTE: Set breakpoint in `lsp_server.py` before continuing.
runpy.run_path(SERVER_PATH, run_name="__main__")
+32
View File
@@ -0,0 +1,32 @@
"""Implementation of tool support over LSP."""
from __future__ import annotations
import copy
import json
import os
import pathlib
import re
import sys
import sysconfig
import traceback
from typing import Any, Optional, Sequence
# **********************************************************
# Update sys.path before importing any bundled libraries.
# **********************************************************
def update_sys_path(path_to_add: str, strategy: str) -> None:
"""Add given path to `sys.path`."""
if path_to_add not in sys.path and os.path.isdir(path_to_add):
if strategy == "useBundled":
sys.path.insert(0, path_to_add)
elif strategy == "fromEnvironment":
sys.path.append(path_to_add)
# Ensure that we can import LSP libraries, and other bundled libraries.
update_sys_path(
os.fspath(pathlib.Path(__file__).parent.parent / "libs"),
os.getenv("LS_IMPORT_STRATEGY", "useBundled"),
)
-1
View File
@@ -1 +0,0 @@
-69
View File
@@ -1,69 +0,0 @@
import {
createConnection,
TextDocuments,
ProposedFeatures,
TextDocumentSyncKind,
InitializeParams,
InitializeResult,
CompletionItemKind,
TextDocumentPositionParams,
CompletionItem,
TextEdit,
Range,
Position
} from "vscode-languageserver/node"
import { TextDocument } from "vscode-languageserver-textdocument"
// Create a connection for the server, using Node's IPC as a transport.
// Also include all preview / proposed LSP features.
let connection = createConnection(ProposedFeatures.all)
// Create a simple text document manager.
let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument)
connection.onInitialize((params: InitializeParams) => {
let capabilities = params.capabilities
// Does the client support the `workspace/configuration` request?
// If not, we fall back using global settings.
const result: InitializeResult = {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
// Tell the client that this server supports code completion.
completionProvider: {
resolveProvider: false
}
}
}
return result
})
// This handler provides the initial list of the completion items.
connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] => {
const doc = documents.get(params.textDocument.uri)
if (!doc) return []
const text = doc.getText()
return []
})
connection.onDocumentFormatting(async (params, token) => {
const document = documents.get(params.textDocument.uri)
if (!document) return []
const originalText = document.getText()
return []
})
documents.onDidChangeContent(async (change) => {
connection.console.log("Document has changed")
})
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection)
// Listen on the connection
connection.listen()