Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #84

Merged
merged 13 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions client/src/lib/src/types/directiveKeywords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2023 Savoir-faire Linux. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

export const DIRECTIVE_STATEMENT_KEYWORDS = ['require', 'inherit', 'include']

export type DirectiveStatementKeyword = 'require' | 'inherit' | 'include'
22 changes: 0 additions & 22 deletions server/src/BasicKeywordMap.ts

This file was deleted.

126 changes: 0 additions & 126 deletions server/src/ContextHandler.ts

This file was deleted.

17 changes: 7 additions & 10 deletions server/src/DefinitionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from './lib/src/types/BitbakeScanResult'

import {
type BitBakeProjectScanner
bitBakeProjectScanner
} from './BitBakeProjectScanner'

import {
Expand All @@ -28,13 +28,8 @@ import { logger } from './lib/src/utils/OutputLogger'
import path from 'path'

export class DefinitionProvider {
private readonly _projectScanner: BitBakeProjectScanner
private _symbolScanner: SymbolScanner | null = null

constructor (projectScanner: BitBakeProjectScanner) {
this._projectScanner = projectScanner
}

// eslint-disable-next-line accessor-pairs
set symbolScanner (symbolScanner: SymbolScanner | null) {
this._symbolScanner = symbolScanner
Expand All @@ -54,7 +49,7 @@ export class DefinitionProvider {
searchString = selectedSympbol
}

const elementInfos: ElementInfo[] = this._projectScanner.classes.filter((obj: ElementInfo): boolean => {
const elementInfos: ElementInfo[] = bitBakeProjectScanner.classes.filter((obj: ElementInfo): boolean => {
return obj.name === searchString
})
definition = this.createDefinitionForElementInfo(elementInfos)
Expand All @@ -65,12 +60,12 @@ export class DefinitionProvider {
case 'include':
{
const includeFile: PathInfo = path.parse(restOfLine)
let elementInfos: ElementInfo[] = this._projectScanner.includes.filter((obj: ElementInfo): boolean => {
let elementInfos: ElementInfo[] = bitBakeProjectScanner.includes.filter((obj: ElementInfo): boolean => {
return obj.name === includeFile.name
})

if (elementInfos.length === 0) {
elementInfos = this._projectScanner.recipes.filter((obj: ElementInfo): boolean => {
elementInfos = bitBakeProjectScanner.recipes.filter((obj: ElementInfo): boolean => {
return obj.name === includeFile.name
})
}
Expand All @@ -97,7 +92,7 @@ export class DefinitionProvider {
private createDefinitionForSymbolRecipes (symbol: string): Definition {
let definitions: Definition = []

const recipe: ElementInfo | undefined = this._projectScanner.recipes.find((obj: ElementInfo): boolean => {
const recipe: ElementInfo | undefined = bitBakeProjectScanner.recipes.find((obj: ElementInfo): boolean => {
return obj.name === symbol
})

Expand Down Expand Up @@ -196,3 +191,5 @@ export class DefinitionProvider {
return Location.create(encodeURI(url), range)
}
}

export const definitionProvider = new DefinitionProvider()
35 changes: 0 additions & 35 deletions server/src/NotificationManager.ts

This file was deleted.

28 changes: 5 additions & 23 deletions server/src/SymbolScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@

import fs from 'fs'

import {
BasicKeywordMap
} from './BasicKeywordMap'

import type {
CompletionItem,
Definition,
Location
} from 'vscode-languageserver'
Expand All @@ -20,6 +15,7 @@ import type {
} from './DefinitionProvider'

import { logger } from './lib/src/utils/OutputLogger'
import { DIRECTIVE_STATEMENT_KEYWORDS } from './lib/src/types/directiveKeywords'
WilsonZiweiWang marked this conversation as resolved.
Show resolved Hide resolved

interface FileContent {
filePath: string
Expand Down Expand Up @@ -65,11 +61,11 @@ export class SymbolScanner {
})

for (const line of file) {
const keyword = this.lineContainsKeyword(line)
const words = line.split(' ')

if (keyword !== undefined) {
logger.debug(`keyword found: ${keyword}`)
this.handleKeyword(keyword, line)
if (new Set(DIRECTIVE_STATEMENT_KEYWORDS).has(words[0])) {
logger.debug(`Directive statement keyword found: ${words[0]}`)
this.handleKeyword(words[0], line)
}
}
} catch (error) {
Expand All @@ -83,20 +79,6 @@ export class SymbolScanner {
}
}

private lineContainsKeyword (line: string): string | undefined {
const keywords: CompletionItem[] = BasicKeywordMap

const trimedLine = line.trim()

for (const keyword of keywords) {
if (trimedLine.startsWith(keyword.label)) {
return keyword.label
}
}

return undefined
}

private handleKeyword (keyword: string, line: string): void {
const restOfLine: string[] = line.split(keyword).filter(String)

Expand Down
4 changes: 4 additions & 0 deletions server/src/__tests__/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ describe('On Completion', () => {
bitBakeDocScanner.clearScannedDocs()
})

afterEach(() => {
jest.restoreAllMocks()
})

it('expects reserved variables, keywords and snippets in completion item lists', async () => {
// nothing is analyzed yet, and docs are not scanned. Only the static and fallback completion items are provided
const result = onCompletionHandler({
Expand Down
11 changes: 7 additions & 4 deletions server/src/__tests__/definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import { analyzer } from '../tree-sitter/analyzer'
import { generateParser } from '../tree-sitter/parser'
import { onDefinitionHandler } from '../connectionHandlers/onDefinition'
import { FIXTURE_DOCUMENT } from './fixtures/fixtures'
import contextHandler from '../ContextHandler'
import { type Location } from 'vscode-languageserver'

import { definitionProvider } from '../DefinitionProvider'
// TODO: Current implementation of the definitionProvider needs to be improved, this test suite should be modified accordingly after
const mockDefinition = (path: string | undefined): void => {
if (path !== undefined) {
const location: Location = { uri: 'file://' + path, range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } } }

jest.spyOn(contextHandler, 'getDefinitionForDirectives').mockReturnValueOnce(location)
jest.spyOn(definitionProvider, 'createDefinitionForKeyword').mockReturnValue(location)
} else {
jest.spyOn(contextHandler, 'getDefinitionForDirectives').mockReturnValueOnce([])
jest.spyOn(definitionProvider, 'createDefinitionForKeyword').mockReturnValue([])
}
}

Expand All @@ -35,6 +34,10 @@ describe('on definition', () => {
analyzer.resetAnalyzedDocuments()
})

afterEach(() => {
jest.resetAllMocks()
})

it('provides definition to directive statement', async () => {
await analyzer.analyze({
uri: DUMMY_URI,
Expand Down
Loading
Loading