Skip to content

Commit

Permalink
feat: Workspace folders support. Sparwn one LSP for each workspace fo…
Browse files Browse the repository at this point in the history
…lder.
  • Loading branch information
zobo committed Mar 14, 2022
1 parent 6b456bf commit 0945964
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 86 deletions.
79 changes: 67 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,52 @@ import * as vscode from 'vscode'
import { LanguageClient, LanguageClientOptions, RevealOutputChannelOn, StreamInfo } from 'vscode-languageclient/node'
const composerJson = require('../composer.json')

const clients: Map<string, LanguageClient> = new Map()

export async function activate(context: vscode.ExtensionContext): Promise<void> {
const conf = vscode.workspace.getConfiguration('php')
if (vscode.workspace.workspaceFolders !== undefined) {
for (const workspaceFolder of vscode.workspace.workspaceFolders) {
const client = await startClient(context, workspaceFolder)
if (client != null) {
clients.set(workspaceFolder.uri.toString(), client)
// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(client.start())
}
}
} else {
const client = await startClient(context, null)
if (client != null) {
// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(client.start())
}
}

vscode.workspace.onDidChangeWorkspaceFolders(async (event) => {
console.log(`onDidChangeWorkspaceFolders ${event.removed} ${event.added}`)
for (const workspaceFolder of event.removed) {
const client = clients.get(workspaceFolder.uri.toString())
if (client) {
clients.delete(workspaceFolder.uri.toString())
client.stop()
}
}
for (const workspaceFolder of event.added) {
const client = await startClient(context, workspaceFolder)
if (client != null) {
clients.set(workspaceFolder.uri.toString(), client)
context.subscriptions.push(client.start())
}
}
})
}

async function startClient(
context: vscode.ExtensionContext,
workspaceFolder: vscode.WorkspaceFolder | null
): Promise<LanguageClient | null> {
const conf = vscode.workspace.getConfiguration('php', workspaceFolder)
const executablePath =
conf.get<string>('executablePath') ||
conf.get<string>('validate.executablePath') ||
Expand All @@ -25,7 +69,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
if (selected === 'Open settings') {
await vscode.commands.executeCommand('workbench.action.openGlobalSettings')
}
return
return null
}

// Check path (if PHP is available and version is ^7.0.0)
Expand All @@ -45,14 +89,14 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.window.showErrorMessage('Error spawning PHP: ' + err.message)
console.error(err)
}
return
return null
}

// Parse version and discard OS info like 7.0.8--0ubuntu0.16.04.2
const match = stdout.match(/^PHP ([^\s]+)/m)
if (!match) {
vscode.window.showErrorMessage('Error parsing PHP version. Please check the output of php --version')
return
return null
}
let version = match[1].split('-')[0]
// Convert PHP prerelease format like 7.0.0rc1 to 7.0.0-rc1
Expand All @@ -63,7 +107,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.window.showErrorMessage(
`The language server needs at least PHP ${composerJson.config.platform.php} installed. Version found: ${version}`
)
return
return null
}

let client: LanguageClient
Expand Down Expand Up @@ -112,10 +156,19 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

// Options to control the language client
const clientOptions: LanguageClientOptions = {
workspaceFolder: workspaceFolder ?? undefined,
// Register the server for php documents
documentSelector: [
{ scheme: 'file', language: 'php' },
{ scheme: 'untitled', language: 'php' },
{
scheme: 'file',
language: 'php',
pattern: workspaceFolder != null ? `${workspaceFolder.uri.fsPath}/**/*` : undefined,
},
{
scheme: 'untitled',
language: 'php',
pattern: workspaceFolder != null ? `${workspaceFolder.uri.fsPath}/**/*` : undefined,
},
],
revealOutputChannelOn: RevealOutputChannelOn.Never,
uriConverters: {
Expand All @@ -133,10 +186,12 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}

// Create the language client and start the client.
client = new LanguageClient('php-intellisense', 'PHP Language Server', serverOptions, clientOptions)
const disposable = client.start()
client = new LanguageClient(
'php-intellisense',
`PHP Language Server (${workspaceFolder?.name ?? 'untitled'})`,
serverOptions,
clientOptions
)

// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disposable)
return client
}
81 changes: 7 additions & 74 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,85 +1,18 @@
{
"extends": ["tslint:recommended", "tslint-config-prettier"],
"extends": ["tslint-config-prettier"],
"rules": {
"adjacent-overload-signatures": true,
"array-type": [true, "array"],
"arrow-return-shorthand": [true, "multiline"],
"await-promise": [true, "Thenable"],
"ban": [
true,
{
"name": ["*", "forEach"]
},
["describe", "only"],
["it", "only"]
],
"callable-types": true,
"class-name": true,
"comment-format": [true, "check-space"],
"curly": true,
"deprecation": {
"severity": "warning"
},
"interface-name": [false],
"interface-over-type-literal": true,
"jsdoc-format": true,
"max-classes-per-file": false,
"member-access": [true, "check-accessor"],
"member-ordering": [false],
"no-angle-bracket-type-assertion": true,
"no-arg": true,
"no-bitwise": false,
"no-boolean-literal-compare": true,
"no-conditional-assignment": true,
"no-console": [false],
"no-construct": true,
"no-debugger": true,
"no-duplicate-super": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-empty-interface": false,
"no-eval": true,
"no-floating-promises": [true],
"no-for-in-array": true,
"no-inferrable-types": [true],
"no-inferred-empty-object-type": true,
"no-internal-module": true,
"no-invalid-template-strings": true,
"no-magic-numbers": false,
"no-misused-new": true,
"no-namespace": [false, "allow-declarations"],
"no-reference-import": true,
"no-shadowed-variable": false,
"no-sparse-arrays": true,
"no-string-literal": true,
"no-string-throw": true,
"no-unbound-method": true,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-qualifier": true,
"no-unnecessary-type-assertion": false,
"no-unsafe-any": false,
"no-unsafe-finally": true,
"no-unused-expression": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"no-var-requires": false,
"no-void-expression": false,
"object-literal-shorthand": true,
"object-literal-sort-keys": false,
"triple-equals": [true, "allow-null-check"],
"variable-name": [true, "ban-keywords"],
"jsdoc-format": true,
"one-variable-per-declaration": [true, "ignore-for-loop"],
"only-arrow-functions": [true, "allow-declarations", "allow-named-functions"],
"prefer-const": [
true,
{
"destructuring": "all"
}
],
"prefer-for-of": true,
"prefer-template": [false, "allow-single-concat"],
"return-undefined": true,
"triple-equals": [true],
"typedef": [true, "call-signature"],
"unified-signatures": true,
"variable-name": [true, "ban-keywords"]
"curly": true,
"no-empty": true,
"no-unused-expression": true
}
}

0 comments on commit 0945964

Please sign in to comment.