From 4d73a6694b05bfa5cd734ea545f951c6cac81499 Mon Sep 17 00:00:00 2001 From: idillon Date: Tue, 2 Jan 2024 20:08:05 -0500 Subject: [PATCH] Chore: Remove debounce on analyze --- server/src/__tests__/analyzer.test.ts | 28 +++++++------- server/src/__tests__/completions.test.ts | 20 +++++----- server/src/__tests__/definition.test.ts | 6 +-- .../src/__tests__/embedded-languages.test.ts | 4 +- server/src/__tests__/hover.test.ts | 12 +++--- server/src/__tests__/semanticTokens.test.ts | 2 +- server/src/server.ts | 4 +- server/src/tree-sitter/analyzer.ts | 17 ++------- server/src/utils/async.ts | 37 ------------------- 9 files changed, 42 insertions(+), 88 deletions(-) delete mode 100644 server/src/utils/async.ts diff --git a/server/src/__tests__/analyzer.test.ts b/server/src/__tests__/analyzer.test.ts index 080bde2a..a2937c16 100644 --- a/server/src/__tests__/analyzer.test.ts +++ b/server/src/__tests__/analyzer.test.ts @@ -32,7 +32,7 @@ describe('analyze', () => { it('analyzes simple correct bb file', async () => { const analyzer = await getAnalyzer() - const diagnostics = await analyzer.analyze({ + const diagnostics = analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.CORRECT }) @@ -41,7 +41,7 @@ describe('analyze', () => { it('analyzes the document and returns global declarations', async () => { const analyzer = await getAnalyzer() - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.DECLARATION }) @@ -128,7 +128,7 @@ describe('analyze', () => { it('analyzes the document and returns word at point', async () => { const analyzer = await getAnalyzer() - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.DECLARATION }) @@ -227,10 +227,10 @@ describe('sourceIncludeFiles', () => { const analyzer = await getAnalyzer() const uri = FIXTURE_URI.DIRECTIVE // analyze 4 documents before calling sourceIncludeFiles - await analyzer.analyze({ document: FIXTURE_DOCUMENT.DIRECTIVE, uri: FIXTURE_URI.DIRECTIVE }) - await analyzer.analyze({ document: FIXTURE_DOCUMENT.BAR_INC, uri: FIXTURE_URI.BAR_INC }) - await analyzer.analyze({ document: FIXTURE_DOCUMENT.FOO_INC, uri: FIXTURE_URI.FOO_INC }) - await analyzer.analyze({ document: FIXTURE_DOCUMENT.BAZ_BBCLASS, uri: FIXTURE_URI.BAZ_BBCLASS }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.DIRECTIVE, uri: FIXTURE_URI.DIRECTIVE }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.BAR_INC, uri: FIXTURE_URI.BAR_INC }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.FOO_INC, uri: FIXTURE_URI.FOO_INC }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.BAZ_BBCLASS, uri: FIXTURE_URI.BAZ_BBCLASS }) jest.spyOn(Analyzer.prototype, 'getDirectiveFileUris').mockReturnValueOnce([ FIXTURE_URI.BAR_INC, @@ -261,10 +261,10 @@ describe('sourceIncludeFiles', () => { const analyzer = await getAnalyzer() const uri = FIXTURE_URI.DIRECTIVE - await analyzer.analyze({ document: FIXTURE_DOCUMENT.DIRECTIVE, uri: FIXTURE_URI.DIRECTIVE }) - await analyzer.analyze({ document: FIXTURE_DOCUMENT.BAR_INC, uri: FIXTURE_URI.BAR_INC }) - await analyzer.analyze({ document: FIXTURE_DOCUMENT.FOO_INC, uri: FIXTURE_URI.FOO_INC }) - await analyzer.analyze({ document: FIXTURE_DOCUMENT.BAZ_BBCLASS, uri: FIXTURE_URI.BAZ_BBCLASS }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.DIRECTIVE, uri: FIXTURE_URI.DIRECTIVE }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.BAR_INC, uri: FIXTURE_URI.BAR_INC }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.FOO_INC, uri: FIXTURE_URI.FOO_INC }) + analyzer.analyze({ document: FIXTURE_DOCUMENT.BAZ_BBCLASS, uri: FIXTURE_URI.BAZ_BBCLASS }) jest.spyOn(Analyzer.prototype, 'getDirectiveFileUris').mockReturnValueOnce([ FIXTURE_URI.BAR_INC, @@ -363,7 +363,7 @@ describe('declarations', () => { const document = FIXTURE_DOCUMENT.COMPLETION const uri = FIXTURE_URI.COMPLETION - await analyzer.analyze({ + analyzer.analyze({ document, uri }) @@ -387,7 +387,7 @@ describe('getLinksInStringContent', () => { const document = FIXTURE_DOCUMENT.CORRECT const uri = FIXTURE_URI.CORRECT - await analyzer.analyze({ + analyzer.analyze({ document, uri }) @@ -412,7 +412,7 @@ describe('getLinksInStringContent', () => { const document = FIXTURE_DOCUMENT.CORRECT const uri = FIXTURE_URI.CORRECT - await analyzer.analyze({ + analyzer.analyze({ document, uri }) diff --git a/server/src/__tests__/completions.test.ts b/server/src/__tests__/completions.test.ts index 0854dad5..db6d002a 100644 --- a/server/src/__tests__/completions.test.ts +++ b/server/src/__tests__/completions.test.ts @@ -178,7 +178,7 @@ describe('On Completion', () => { }) it("doesn't provide suggestions when it is pure string content", async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -197,7 +197,7 @@ describe('On Completion', () => { }) it("doesn't provide duplicate completion items for local custom variables", async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -223,7 +223,7 @@ describe('On Completion', () => { }) it('provides necessary suggestions when it is in variable expansion', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -269,7 +269,7 @@ describe('On Completion', () => { }) // TODO: Add tests for the second and other overrides that come after. e.g. VAR:override1:override2:over... And it is better after the tree-sitter library can properly handle it as mentioned in the issue: https://github.com/amaanq/tree-sitter-bitbake/issues/9 it('provides suggestions for operators when a ":" is typed and it follows an identifier or in the middle of typing such syntax', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -353,7 +353,7 @@ describe('On Completion', () => { }) it('provides suggestions for overrides when a ":" is typed and it follows an identifier', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -383,7 +383,7 @@ describe('On Completion', () => { }) it('provides no suggestions when a ":" is typed but it is not part of a valid override syntax', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -402,7 +402,7 @@ describe('On Completion', () => { }) it('provides suggestions for variable flags when a "[" is typed and it follows an identifier (Before and After scanning docs', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -458,7 +458,7 @@ describe('On Completion', () => { }) it('provides no suggestions when a "[" is typed but it doesn\'t follow a bitbake identifier', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -541,7 +541,7 @@ describe('On Completion', () => { } }] - await analyzer.analyze({ + analyzer.analyze({ uri: documentUri, document: FIXTURE_DOCUMENT.COMPLETION }) @@ -648,7 +648,7 @@ describe('On Completion', () => { _recipes: [] } - await analyzer.analyze({ + analyzer.analyze({ uri: FIXTURE_URI.DIRECTIVE, document: FIXTURE_DOCUMENT.DIRECTIVE }) diff --git a/server/src/__tests__/definition.test.ts b/server/src/__tests__/definition.test.ts index 045ccfa6..5c1ef44c 100644 --- a/server/src/__tests__/definition.test.ts +++ b/server/src/__tests__/definition.test.ts @@ -57,7 +57,7 @@ describe('on definition', () => { _recipes: [] } - await analyzer.analyze({ + analyzer.analyze({ uri: FIXTURE_URI.DIRECTIVE, document: FIXTURE_DOCUMENT.DIRECTIVE }) @@ -119,7 +119,7 @@ describe('on definition', () => { ] } - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.DIRECTIVE }) @@ -179,7 +179,7 @@ describe('on definition', () => { } ] - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.DIRECTIVE }) diff --git a/server/src/__tests__/embedded-languages.test.ts b/server/src/__tests__/embedded-languages.test.ts index 46a85296..fb6ff74b 100644 --- a/server/src/__tests__/embedded-languages.test.ts +++ b/server/src/__tests__/embedded-languages.test.ts @@ -31,7 +31,7 @@ describe('Embedded Language Documents file management', () => { it('generate, rename and delete embedded language documents', async () => { // Setup - await analyzer.analyze({ + analyzer.analyze({ uri: FIXTURE_DOCUMENT.EMBEDDED.uri, document: FIXTURE_DOCUMENT.EMBEDDED }) @@ -198,7 +198,7 @@ describe('Create Python embedded language content with inline Python', () => { const createEmbeddedContent = async (content: string, language: EmbeddedLanguageType): Promise => { const uri = randomUUID() const document = TextDocument.create(uri, 'bitbake', 1, content) - await analyzer.analyze({ document, uri }) + analyzer.analyze({ document, uri }) await generateEmbeddedLanguageDocs(document) const pythonEmbeddedLanguageDocInfos = embeddedLanguageDocsManager.getEmbeddedLanguageDocInfos(uri, language) if (pythonEmbeddedLanguageDocInfos === undefined) { diff --git a/server/src/__tests__/hover.test.ts b/server/src/__tests__/hover.test.ts index 357ed7c4..af577333 100644 --- a/server/src/__tests__/hover.test.ts +++ b/server/src/__tests__/hover.test.ts @@ -27,7 +27,7 @@ describe('on hover', () => { it('shows definition on hovering variable in variable assignment syntax or in variable expansion syntax after scanning the docs', async () => { bitBakeDocScanner.parseBitbakeVariablesFile() - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.HOVER }) @@ -140,7 +140,7 @@ describe('on hover', () => { it('should show hover definition for variable flags after scanning the docs', async () => { bitBakeDocScanner.parseVariableFlagFile() - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.HOVER }) @@ -177,7 +177,7 @@ describe('on hover', () => { it('should show hover definition for yocto tasks after scanning the docs', async () => { bitBakeDocScanner.parseYoctoTaskFile() - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.HOVER }) @@ -258,7 +258,7 @@ describe('on hover', () => { }) it('should show hover definition for keywords', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.HOVER }) @@ -321,7 +321,7 @@ describe('on hover', () => { it('shows definition on hovering variable in Python functions for accessing datastore', async () => { bitBakeDocScanner.parseBitbakeVariablesFile() bitBakeDocScanner.parsePythonDatastoreFunction() - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.HOVER }) @@ -490,7 +490,7 @@ describe('on hover', () => { _recipes: [] } - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.DIRECTIVE }) diff --git a/server/src/__tests__/semanticTokens.test.ts b/server/src/__tests__/semanticTokens.test.ts index b88f3ad8..76193052 100644 --- a/server/src/__tests__/semanticTokens.test.ts +++ b/server/src/__tests__/semanticTokens.test.ts @@ -22,7 +22,7 @@ describe('Semantic tokens', () => { }) it('gives approriate semantic tokens to symbols', async () => { - await analyzer.analyze({ + analyzer.analyze({ uri: DUMMY_URI, document: FIXTURE_DOCUMENT.SEMANTIC_TOKENS }) diff --git a/server/src/server.ts b/server/src/server.ts index 8fe9a5e2..947c4a8d 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -63,7 +63,7 @@ connection.onInitialize(async (params: InitializeParams): Promise { logger.debug('[On scanReady] Analyzing the current document again...') - void analyzer.analyze({ document: currentActiveTextDocument, uri: currentActiveTextDocument.uri }) + analyzer.analyze({ document: currentActiveTextDocument, uri: currentActiveTextDocument.uri }) }) return { @@ -134,7 +134,7 @@ documents.onDidChangeContent(async (event) => { const textDocument = event.document if (textDocument.getText().length > 0) { - const diagnostics = await analyzer.analyze({ document: textDocument, uri: textDocument.uri }) + const diagnostics = analyzer.analyze({ document: textDocument, uri: textDocument.uri }) void generateEmbeddedLanguageDocs(event.document) void connection.sendDiagnostics({ uri: textDocument.uri, diagnostics }) } diff --git a/server/src/tree-sitter/analyzer.ts b/server/src/tree-sitter/analyzer.ts index 003c84ae..4bbe47a0 100644 --- a/server/src/tree-sitter/analyzer.ts +++ b/server/src/tree-sitter/analyzer.ts @@ -19,7 +19,6 @@ import { import type Parser from 'web-tree-sitter' import { TextDocument } from 'vscode-languageserver-textdocument' import { type EmbeddedRegions, getEmbeddedRegionsFromNode, getGlobalDeclarationsAndComments, type GlobalDeclarations, type GlobalSymbolComments } from './declarations' -import { debounce } from '../utils/async' import { type Tree } from 'web-tree-sitter' import * as TreeSitterUtils from './utils' import { type DirectiveStatementKeyword } from '../lib/src/types/directiveKeywords' @@ -28,7 +27,6 @@ import fs from 'fs' import path from 'path' import { bitBakeProjectScannerClient } from '../BitbakeProjectScannerClient' import { bitBakeDocScanner } from '../BitBakeDocScanner' -const DEBOUNCE_TIME_MS = 500 interface AnalyzedDocument { document: TextDocument @@ -43,7 +41,6 @@ interface AnalyzedDocument { export default class Analyzer { private parser?: Parser private uriToAnalyzedDocument: Record = {} - private debouncedExecuteAnalyzation?: ReturnType public getDocumentTexts (uri: string): string[] | undefined { return this.uriToAnalyzedDocument[uri]?.document.getText().split(/\r?\n/g) @@ -61,16 +58,16 @@ export default class Analyzer { this.parser = parser } - public async analyze ({ + public analyze ({ document, uri }: { document: TextDocument uri: string - }): Promise { + }): Diagnostic[] { if (this.parser === undefined) { logger.debug('[Analyzer] The analyzer is not initialized with a parser') - return await Promise.resolve([]) + return [] } const fileContent = document.getText() @@ -95,13 +92,7 @@ export default class Analyzer { extraSymbols } - let debouncedExecuteAnalyzation = this.debouncedExecuteAnalyzation - if (debouncedExecuteAnalyzation === undefined) { - debouncedExecuteAnalyzation = debounce(this.executeAnalyzation.bind(this), DEBOUNCE_TIME_MS) - this.debouncedExecuteAnalyzation = debouncedExecuteAnalyzation - } - - return await debouncedExecuteAnalyzation(document, uri, tree) + return this.executeAnalyzation(document, uri, tree) } private executeAnalyzation (document: TextDocument, uri: string, tree: Tree): Diagnostic[] { diff --git a/server/src/utils/async.ts b/server/src/utils/async.ts deleted file mode 100644 index e0e77955..00000000 --- a/server/src/utils/async.ts +++ /dev/null @@ -1,37 +0,0 @@ -/* -------------------------------------------------------------------------------------------- - * Copyright (c) 2023 Savoir-faire Linux. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - * ------------------------------------------------------------------------------------------ */ - -/** - * Inspired by bash-language-server under MIT - * Reference: https://github.com/bash-lsp/bash-language-server/blob/8c42218c77a9451b308839f9a754abde901323d5/server/src/util/async.ts - */ - -/** - * A collection of async utilities. - * If we ever need to add anything fancy, then https://github.com/vscode-shellcheck/vscode-shellcheck/blob/master/src/utils/async.ts - * is a good place to look. - */ -type UnwrapPromise = T extends Promise ? U : T - -/** - * Debounce a function call by a given amount of time. Only the last call - * will be resolved. - * Inspired by https://gist.github.com/ca0v/73a31f57b397606c9813472f7493a940 - */ -export const debounce = any>( - func: F, - waitForMs: number -): (...args: Parameters) => Promise>> => { - let timeout: ReturnType | null = null - - return async (...args: Parameters): Promise>> => - await new Promise((resolve) => { - if (timeout !== null) { - clearTimeout(timeout) - } - - timeout = setTimeout(() => { resolve(func(...args)) }, waitForMs) - }) -}