支持全局函数补全
This commit is contained in:
+84
-51
@@ -1,75 +1,108 @@
|
||||
import * as path from 'path'
|
||||
import * as vscode from 'vscode'
|
||||
import * as fs from 'fs'
|
||||
import {LanguageClient, LanguageClientOptions, ServerOptions, TransportKind} from 'vscode-languageclient/node'
|
||||
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node'
|
||||
|
||||
let client: LanguageClient
|
||||
|
||||
function findSystemTslServer(): string | null {
|
||||
const binary = process.platform === 'win32' ? 'tsl-server.exe' : 'tsl-server'
|
||||
const pathDirs = process.env.PATH?.split(path.delimiter) || []
|
||||
function findInSystemPath(binary: string): string | null {
|
||||
const pathDirs = process.env.PATH?.split(path.delimiter) || []
|
||||
|
||||
for (const dir of pathDirs) {
|
||||
const fullPath = path.join(dir, binary)
|
||||
if (fs.existsSync(fullPath)) {
|
||||
return fullPath
|
||||
for (const dir of pathDirs) {
|
||||
const fullPath = path.join(dir, binary)
|
||||
if (fs.existsSync(fullPath)) {
|
||||
return fullPath
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return null
|
||||
function findExecutable(context: vscode.ExtensionContext, configPath: string | undefined, bundledRelativePath: string, systemBinaryName: string): string | null {
|
||||
// 1. 优先使用用户配置的路径
|
||||
if (configPath) {
|
||||
return configPath
|
||||
}
|
||||
|
||||
// 2. 查找插件自带的可执行文件
|
||||
const bundledPath = context.asAbsolutePath(bundledRelativePath)
|
||||
if (fs.existsSync(bundledPath)) {
|
||||
return bundledPath
|
||||
}
|
||||
|
||||
// 3. 从系统 PATH 查找
|
||||
return findInSystemPath(systemBinaryName)
|
||||
}
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
// server配置项
|
||||
const config = vscode.workspace.getConfiguration('tsl')
|
||||
const customExecutable = config.get<string>('server.executable', '')
|
||||
const serverArguments = config.get<string[]>('server.arguments')
|
||||
const config = vscode.workspace.getConfiguration('tsl')
|
||||
const serverArguments = config.get<string[]>('server.arguments') || []
|
||||
|
||||
let serverExe: string | null = null
|
||||
|
||||
if (customExecutable) {
|
||||
serverExe = customExecutable
|
||||
} else {
|
||||
const bundledPath = context.asAbsolutePath(
|
||||
path.join('bin', process.platform === 'win32' ? 'tsl-server.exe' : 'tsl-server')
|
||||
const serverBinary = process.platform === 'win32' ? 'tsl-server.exe' : 'tsl-server'
|
||||
let serverExe = findExecutable(
|
||||
context,
|
||||
config.get<string>('server.executable'),
|
||||
path.join('bin', serverBinary),
|
||||
serverBinary
|
||||
)
|
||||
// 查找路径优先级:插件目录 -> 系统 PATH
|
||||
if (fs.existsSync(bundledPath)) {
|
||||
serverExe = bundledPath
|
||||
} else {
|
||||
serverExe = findSystemTslServer()
|
||||
}
|
||||
}
|
||||
|
||||
if (!serverExe) {
|
||||
vscode.window.showErrorMessage(
|
||||
"Cannot find tsl-server. Please install it globally or include it in your extension's server directory."
|
||||
if (!serverExe) {
|
||||
vscode.window.showErrorMessage(
|
||||
"Cannot find tsl-server. Please install it globally or configure 'tsl.server.executable' in settings."
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const interpreterBinary = process.platform === 'win32' ? 'tsl.exe' : 'tsl'
|
||||
let interpreterExe = findExecutable(
|
||||
context,
|
||||
config.get<string>('interpreter.executable'),
|
||||
"",
|
||||
interpreterBinary
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const serverOptions: ServerOptions = {
|
||||
run: {command: serverExe, transport: TransportKind.stdio, args: serverArguments},
|
||||
debug: {command: serverExe, transport: TransportKind.stdio, args: ['--log=trace', '--log-stderr']}
|
||||
}
|
||||
|
||||
const clientOptions: LanguageClientOptions = {
|
||||
documentSelector: [{scheme: 'file', language: 'tsl'}],
|
||||
synchronize: {
|
||||
fileEvents: vscode.workspace.createFileSystemWatcher('**/*.tsl')
|
||||
if (!interpreterExe) {
|
||||
vscode.window.showWarningMessage(
|
||||
"Cannot find TSL interpreter. Some features may not work. Please install it globally or configure 'tsl.interpreter.executable' in settings."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
client = new LanguageClient(
|
||||
'tslLanguageServer', // 客户端唯一ID
|
||||
'TSL Language Server', // 用户可见名称
|
||||
serverOptions,
|
||||
clientOptions
|
||||
)
|
||||
const runArgs = [...serverArguments]
|
||||
const debugArgs = ['--log=trace', '--log-stderr']
|
||||
|
||||
client.start()
|
||||
if (interpreterExe) {
|
||||
let interpreterDir = path.dirname(interpreterExe)
|
||||
interpreterDir = interpreterDir.replace(/\\/g, '/') + '/'
|
||||
runArgs.push(`--interpreter=${interpreterDir}`)
|
||||
debugArgs.push(`--interpreter=${interpreterDir}`)
|
||||
}
|
||||
|
||||
const serverOptions: ServerOptions = {
|
||||
run: { command: serverExe, transport: TransportKind.stdio, args: runArgs },
|
||||
debug: { command: serverExe, transport: TransportKind.stdio, args: debugArgs }
|
||||
}
|
||||
|
||||
const clientOptions: LanguageClientOptions = {
|
||||
documentSelector: [{ scheme: 'file', language: 'tsl' }],
|
||||
synchronize: {
|
||||
fileEvents: vscode.workspace.createFileSystemWatcher('**/*.tsl')
|
||||
}
|
||||
}
|
||||
|
||||
client = new LanguageClient(
|
||||
'tslLanguageServer',
|
||||
'TSL Language Server',
|
||||
serverOptions,
|
||||
clientOptions
|
||||
)
|
||||
|
||||
// 保存解释器路径到上下文,供其他命令使用
|
||||
if (interpreterExe) {
|
||||
context.globalState.update('tsl.interpreterPath', interpreterExe)
|
||||
}
|
||||
|
||||
client.start()
|
||||
}
|
||||
|
||||
export function deactivate(): Thenable<void> | undefined {
|
||||
return client ? client.stop() : undefined
|
||||
return client ? client.stop() : undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user