Skip to content

Commit 61e8a58

Browse files
committed
update
1 parent 6cf9dce commit 61e8a58

File tree

16 files changed

+213
-418
lines changed

16 files changed

+213
-418
lines changed

packages/amazonq/src/lsp/activation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
import vscode from 'vscode'
77
import { startLanguageServer } from './client'
8-
import { AmazonQLSPInstaller } from './lspInstaller'
8+
import { AmazonQLSPResolver } from './lspInstaller'
99
import { ToolkitError } from 'aws-core-vscode/shared'
10+
import path from 'path'
1011

1112
export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
1213
try {
13-
const result = await new AmazonQLSPInstaller().install()
14-
await startLanguageServer(ctx, result.location)
14+
const installResult = await new AmazonQLSPResolver().resolve()
15+
await startLanguageServer(ctx, path.join(installResult.assetDirectory, 'servers/aws-lsp-codewhisperer.js'))
1516
} catch (err) {
1617
const e = err as ToolkitError
1718
void vscode.window.showInformationMessage(`Unable to launch amazonq language server: ${e.message}`)

packages/amazonq/src/lsp/lspInstaller.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
*/
55

66
import * as vscode from 'vscode'
7-
import { ManifestManager, LspManager, LspInstaller, LspResult } from 'aws-core-vscode/shared'
7+
import { Range } from 'semver'
8+
import { ManifestResolver, LanguageServerResolver, LspResolver, LspResult } from 'aws-core-vscode/shared'
89

910
const manifestURL = 'https://aws-toolkit-language-servers.amazonaws.com/codewhisperer/0/manifest.json'
11+
const supportedLspServerVersions = '^2.3.1'
1012

11-
export class AmazonQLSPInstaller implements LspInstaller {
12-
async install(): Promise<LspResult> {
13+
export class AmazonQLSPResolver implements LspResolver {
14+
async resolve(): Promise<LspResult> {
1315
const overrideLocation = process.env.AWS_LANGUAGE_SERVER_OVERRIDE
1416
if (overrideLocation) {
1517
void vscode.window.showInformationMessage(`Using language server override location: ${overrideLocation}`)
@@ -20,13 +22,16 @@ export class AmazonQLSPInstaller implements LspInstaller {
2022
}
2123
}
2224

23-
const manifestManager = new ManifestManager(manifestURL, 'amazonq')
24-
const manifest = await manifestManager.getManifest()
25-
26-
const lspManager = new LspManager(manifest, '', '')
27-
const downloadResult = lspManager.download()
25+
// "AmazonQ" is shared across toolkits to provide a common access point, don't change it
26+
const name = 'AmazonQ'
27+
const manifest = await new ManifestResolver(manifestURL, name).resolve()
28+
const installationResult = await new LanguageServerResolver(
29+
manifest,
30+
name,
31+
new Range(supportedLspServerVersions)
32+
).resolve()
2833

2934
// TODO Cleanup old versions of language servers
30-
return downloadResult
35+
return installationResult
3136
}
3237
}

packages/amazonq/test/unit/amazonq/lsp/lspController.test.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

packages/core/src/amazonq/lsp/lspClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,11 @@ export class LspClient {
172172
* It will create a output channel named Amazon Q Language Server.
173173
* This function assumes the LSP server has already been downloaded.
174174
*/
175-
export async function activate(extensionContext: ExtensionContext) {
175+
export async function activate(extensionContext: ExtensionContext, serverModule: string) {
176176
LspClient.instance
177177
const toDispose = extensionContext.subscriptions
178178

179179
let rangeFormatting: Disposable | undefined
180-
// The server is implemented in node
181-
const serverModule = path.join(extensionContext.extensionPath, 'resources/qserver/lspServer.js')
182180
// The debug options for the server
183181
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
184182
const debugOptions = { execArgv: ['--nolazy', '--preserve-symlinks', '--stdio'] }

packages/core/src/amazonq/lsp/lspController.ts

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import { telemetry } from '../../shared/telemetry'
1414
import { isCloud9 } from '../../shared/extensionUtilities'
1515
import globals, { isWeb } from '../../shared/extensionGlobals'
1616
import { isAmazonInternalOs } from '../../shared/vscode/env'
17-
import { fs } from '../../shared/fs/fs'
18-
import { tryRemoveFolder } from '../../shared/filesystemUtilities'
19-
import { LspInstaller, LspResult } from '../../shared/languageServer/types'
17+
import { WorkspaceLSPResolver } from './workspaceInstaller'
2018

2119
export interface Chunk {
2220
readonly filePath: string
@@ -25,13 +23,6 @@ export interface Chunk {
2523
readonly relativePath?: string
2624
readonly programmingLanguage?: string
2725
}
28-
29-
// const manifestUrl = 'https://aws-toolkit-language-servers.amazonaws.com/q-context/manifest.json'
30-
// // this LSP client in Q extension is only going to work with these LSP server versions
31-
// const supportedLspServerVersions = ['0.1.32']
32-
33-
const nodeBinName = process.platform === 'win32' ? 'node.exe' : 'node'
34-
3526
export interface BuildIndexConfig {
3627
startUrl?: string
3728
maxIndexSize: number
@@ -49,21 +40,14 @@ export interface BuildIndexConfig {
4940
* Pre-process the input to Index Files API
5041
* Post-process the output from Query API
5142
*/
52-
export class LspController implements LspInstaller {
43+
export class LspController {
5344
static #instance: LspController
5445
private _isIndexingInProgress = false
55-
private serverPath: string
56-
private nodePath: string
5746

5847
public static get instance() {
5948
return (this.#instance ??= new this())
6049
}
6150

62-
constructor() {
63-
this.serverPath = globals.context.asAbsolutePath(path.join('resources', 'qserver'))
64-
this.nodePath = globals.context.asAbsolutePath(path.join('resources', nodeBinName))
65-
}
66-
6751
isIndexingInProgress() {
6852
return this._isIndexingInProgress
6953
}
@@ -169,40 +153,16 @@ export class LspController implements LspInstaller {
169153
}
170154
}
171155

172-
async isLspInstalled(): Promise<boolean> {
173-
return (await fs.exists(this.serverPath)) && (await fs.exists(this.nodePath))
174-
}
175-
176-
async cleanup(): Promise<boolean> {
177-
if (await fs.exists(this.serverPath)) {
178-
await tryRemoveFolder(this.serverPath)
179-
}
180-
181-
if (await fs.exists(this.nodePath)) {
182-
await fs.delete(this.nodePath)
183-
}
184-
185-
return true
186-
}
187-
188-
install(): Promise<LspResult> {
189-
// TODO
190-
throw new Error('Method not implemented.')
191-
}
192-
193156
async trySetupLsp(context: vscode.ExtensionContext, buildIndexConfig: BuildIndexConfig) {
194157
if (isCloud9() || isWeb() || isAmazonInternalOs()) {
195158
getLogger().warn('LspController: Skipping LSP setup. LSP is not compatible with the current environment. ')
196159
// do not do anything if in Cloud9 or Web mode or in AL2 (AL2 does not support node v18+)
197160
return
198161
}
199162
setImmediate(async () => {
200-
const ok = await LspController.instance.install()
201-
if (!ok) {
202-
return
203-
}
204163
try {
205-
await activateLsp(context)
164+
const installResult = await new WorkspaceLSPResolver().resolve()
165+
await activateLsp(context, path.join(installResult.assetDirectory, 'resources/qserver/lspServer.js'))
206166
getLogger().info('LspController: LSP activated')
207167
void LspController.instance.buildIndex(buildIndexConfig)
208168
// log the LSP server CPU and Memory usage per 30 minutes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { LspResolver, LspResult } from '../../shared/languageServer/types'
7+
import { ManifestResolver } from '../../shared/languageServer/manifestResolver'
8+
import { LanguageServerResolver } from '../../shared/languageServer/lspResolver'
9+
import { Range } from 'semver'
10+
11+
const manifestUrl = 'https://aws-toolkit-language-servers.amazonaws.com/q-context/manifest.json'
12+
// this LSP client in Q extension is only going to work with these LSP server versions
13+
const supportedLspServerVersions = '0.1.32'
14+
15+
export class WorkspaceLSPResolver implements LspResolver {
16+
async resolve(): Promise<LspResult> {
17+
const name = 'AmazonQ-Workspace'
18+
const manifest = await new ManifestResolver(manifestUrl, name).resolve()
19+
const installationResult = await new LanguageServerResolver(
20+
manifest,
21+
name,
22+
new Range(supportedLspServerVersions)
23+
).resolve()
24+
25+
// TODO Cleanup old versions of language servers
26+
return installationResult
27+
}
28+
}

packages/core/src/shared/crypto.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
import { isWeb } from './extensionGlobals'
2525

2626
export function randomUUID(): `${string}-${string}-${string}-${string}-${string}` {
27+
return getCrypto().randomUUID()
28+
}
29+
30+
function getCrypto() {
2731
if (isWeb()) {
28-
return globalThis.crypto.randomUUID()
32+
return globalThis.crypto
2933
}
3034

31-
return require('crypto').randomUUID()
35+
return require('crypto')
3236
}
3337

3438
/**
@@ -54,3 +58,10 @@ export function truncateUuid(uuid: string) {
5458
const cleanedUUID = uuid.replace(/-/g, '')
5559
return `${cleanedUUID.substring(0, 4)}...${cleanedUUID.substring(cleanedUUID.length - 4)}`
5660
}
61+
62+
export function createHash(algorithm: string, contents: string | Buffer): string {
63+
const crypto = getCrypto()
64+
const hash = crypto.createHash(algorithm)
65+
hash.update(contents)
66+
return `${algorithm}:${hash.digest('hex')}`
67+
}

packages/core/src/shared/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ export { i18n } from './i18n-helper'
5959
export * from './icons'
6060
export * as textDocumentUtil from './utilities/textDocumentUtilities'
6161
export { TabTypeDataMap } from '../amazonq/webview/ui/tabs/constants'
62-
export * from './languageServer/manifestManager'
63-
export * from './languageServer/lspManager'
62+
export * from './languageServer/manifestResolver'
63+
export * from './languageServer/lspResolver'
6464
export * from './languageServer/types'

0 commit comments

Comments
 (0)