Skip to content

Commit

Permalink
Chore: Add lazy load to spdx-licenses.json
Browse files Browse the repository at this point in the history
  • Loading branch information
idillon-sfl committed Jul 5, 2024
1 parent aa6cb6b commit aa3def5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 48 deletions.
74 changes: 37 additions & 37 deletions server/src/__tests__/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('On Completion', () => {

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({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('On Completion', () => {
bitBakeDocScanner.parseYoctoVariablesFile()
bitBakeDocScanner.parseYoctoTaskFile()

const resultAfterDocScan = onCompletionHandler({
const resultAfterDocScan = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.DIRECTIVE
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -262,7 +262,7 @@ describe('On Completion', () => {

analyzer.setRecipeLocalFiles(DUMMY_URI, recipeLocalFiles)

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -305,7 +305,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -331,7 +331,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result1 = onCompletionHandler({
const result1 = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -341,7 +341,7 @@ describe('On Completion', () => {
}
})
// Empty ${}
const result2 = onCompletionHandler({
const result2 = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -377,7 +377,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -388,7 +388,7 @@ describe('On Completion', () => {
})

// In the middle of typing operator/override syntax
const result2 = onCompletionHandler({
const result2 = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -398,7 +398,7 @@ describe('On Completion', () => {
}
})
// MYVAR:append: = '123' when the cursor is at the end of second colon
const result3 = onCompletionHandler({
const result3 = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -408,7 +408,7 @@ describe('On Completion', () => {
}
})
// Show completion at the last line of the document https://github.com/amaanq/tree-sitter-bitbake/issues/9
const result4 = onCompletionHandler({
const result4 = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -463,7 +463,7 @@ describe('On Completion', () => {

bitBakeProjectScannerClient.bitbakeScanResult._overrides = ['class-target']

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -491,7 +491,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -510,7 +510,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -531,7 +531,7 @@ describe('On Completion', () => {

bitBakeDocScanner.parseVariableFlagFile()

const resultAfterDocScan = onCompletionHandler({
const resultAfterDocScan = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -566,7 +566,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result1 = onCompletionHandler({
const result1 = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -576,7 +576,7 @@ describe('On Completion', () => {
}
})

const result2 = onCompletionHandler({
const result2 = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -666,7 +666,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const resultForInclude = onCompletionHandler({
const resultForInclude = await onCompletionHandler({
textDocument: {
uri: documentUri
},
Expand All @@ -676,7 +676,7 @@ describe('On Completion', () => {
}
})

const resultForRequire = onCompletionHandler({
const resultForRequire = await onCompletionHandler({
textDocument: {
uri: documentUri
},
Expand All @@ -686,7 +686,7 @@ describe('On Completion', () => {
}
})

const resultForInherit = onCompletionHandler({
const resultForInherit = await onCompletionHandler({
textDocument: {
uri: documentUri
},
Expand All @@ -696,7 +696,7 @@ describe('On Completion', () => {
}
})

const resultForInheritDefer = onCompletionHandler({
const resultForInheritDefer = await onCompletionHandler({
textDocument: {
uri: documentUri
},
Expand Down Expand Up @@ -805,7 +805,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.DIRECTIVE
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: FIXTURE_URI.DIRECTIVE
},
Expand Down Expand Up @@ -846,7 +846,7 @@ describe('On Completion', () => {
bitBakeDocScanner.parseBitbakeVariablesFile()
bitBakeDocScanner.parseYoctoVariablesFile()

const result2 = onCompletionHandler({
const result2 = await onCompletionHandler({
textDocument: {
uri: FIXTURE_URI.DIRECTIVE
},
Expand Down Expand Up @@ -903,7 +903,7 @@ describe('On Completion', () => {
bitBakeDocScanner.parsePythonDatastoreFunction()

// Completion items on string_start node
const resultOnStringStart = onCompletionHandler({
const resultOnStringStart = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -962,7 +962,7 @@ describe('On Completion', () => {
).toBeUndefined()

// Completion items on string_content node
const resultOnStringContent = onCompletionHandler({
const resultOnStringContent = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -1030,7 +1030,7 @@ describe('On Completion', () => {
bitBakeDocScanner.parseBitbakeVariablesFile()
bitBakeDocScanner.parseYoctoVariablesFile()

const resultInVariableExpansion = onCompletionHandler({
const resultInVariableExpansion = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -1057,7 +1057,7 @@ describe('On Completion', () => {
])
)

const resultNotInVariableExpansion = onCompletionHandler({
const resultNotInVariableExpansion = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -1080,7 +1080,7 @@ describe('On Completion', () => {
bitBakeDocScanner.parseBitbakeVariablesFile()
bitBakeDocScanner.parseYoctoVariablesFile()

const resultInVariableExpansion = onCompletionHandler({
const resultInVariableExpansion = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -1119,7 +1119,7 @@ describe('On Completion', () => {
bitBakeDocScanner.parseYoctoTaskFile()
bitBakeDocScanner.parsePythonDatastoreFunction()

const resultOnVariableExpansion = onCompletionHandler({
const resultOnVariableExpansion = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -1136,7 +1136,7 @@ describe('On Completion', () => {
expect(pnCompletionItems.length).toBe(1) // not duplicated with common directories
expect(pnCompletionItems[0].labelDetails?.description).toBe('Source: Yocto') // Not overridden by common directories

const resultOnPythonDatastoreVariable = onCompletionHandler({
const resultOnPythonDatastoreVariable = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -1149,7 +1149,7 @@ describe('On Completion', () => {
resultOnPythonDatastoreVariable.find((item) => item.label === 'DEBIAN_MIRROR')
).toBeDefined()

const resultOnVariableAssignation = onCompletionHandler({
const resultOnVariableAssignation = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand All @@ -1171,7 +1171,7 @@ describe('On Completion', () => {

bitBakeDocScanner.parseYoctoVariablesFile()

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -1205,7 +1205,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -1235,7 +1235,7 @@ describe('On Completion', () => {

analyzer.processRecipeScanResults(scanResults, extractRecipeName(DUMMY_URI))

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -1266,7 +1266,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const resultBeforeResolve = onCompletionHandler({
const resultBeforeResolve = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down Expand Up @@ -1322,7 +1322,7 @@ describe('On Completion', () => {
document: FIXTURE_DOCUMENT.COMPLETION
})

const result = onCompletionHandler({
const result = await onCompletionHandler({
textDocument: {
uri: DUMMY_URI
},
Expand Down
37 changes: 30 additions & 7 deletions server/src/completions/spdx-licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,38 @@ export interface SpdxLicenseDetails {
}

export const spdxLicenseDescription = 'Source: SPDX License List'
const cache = new NodeCache()

const spdxLicensesPath = path.join(__dirname, '../../resources/spdx-licenses.json')
const spdxLicensesFileContent = fs.readFileSync(spdxLicensesPath, 'utf8')
export const spdxLicensesCollection = JSON.parse(spdxLicensesFileContent) as SpdxLicenseCollection
export const spdxLicenses = spdxLicensesCollection.licenses
const loadSpdxLicenses = async (): Promise<SpdxLicense[]> => {
logger.debug('[loadSpdxLicenses] Load SPDX licenses')
return await new Promise<SpdxLicense[]>((resolve) => {
const spdxLicensesPath = path.join(__dirname, '../../resources/spdx-licenses.json')
fs.readFile(spdxLicensesPath, (error, data) => {
if (error !== null) {
logger.error(`[loadSpdxLicenses] error: ${JSON.stringify(error)}`)
resolve([])
}
const spdxLicensesCollection = JSON.parse(data.toString()) as SpdxLicenseCollection
resolve(spdxLicensesCollection.licenses)
})
})
}

export const getLicenseCompletionItems = (rangeOfText: Range): CompletionItem[] => {
const getSpdxLicenses = async (): Promise<SpdxLicense[]> => {
logger.debug('[getSpdxLicenses] Get SPDX licenses')
const cacheKey = 'spdxLicenses'
// node-cache will eventually release the memory, compared to a simple global variable.
const cachedSpdxLicenses = cache.get<SpdxLicense[]>(cacheKey)
if (cachedSpdxLicenses !== undefined) {
return cachedSpdxLicenses
}
const spdxLicenses = await loadSpdxLicenses()
cache.set(cacheKey, spdxLicenses)
return spdxLicenses
}

export const getLicenseCompletionItems = async (rangeOfText: Range): Promise<CompletionItem[]> => {
const spdxLicenses = await getSpdxLicenses()
return spdxLicenses.map((license) => (
{
label: license.licenseId,
Expand All @@ -77,8 +102,6 @@ export const getLicenseCompletionItems = (rangeOfText: Range): CompletionItem[]
))
}

const cache = new NodeCache()

export const getSpdxLicenseCompletionResolve = async (item: CompletionItem): Promise<CompletionItem> => {
try {
const license = item.data.payload as SpdxLicense
Expand Down
Loading

0 comments on commit aa3def5

Please sign in to comment.