Tom Butcher 24c06c25d3 Refactor JavaScript Language Support and Enhance Autocomplete Functionality
- Consolidated JavaScript completion sources into a new module, improving organization and maintainability.
- Updated the `fcTemplateLang` to utilize the new `javascriptCompletionSources` for enhanced autocomplete capabilities.
- Removed redundant imports and streamlined the code in various components, including Invoices, Payments, TaxRecords, and Inventory sections, by replacing dropdown actions with a unified `ObjectActions` component.
- Improved the handling of object actions across multiple inventory and finance components, enhancing user experience and code consistency.
2026-08-20 19:35:09 +01:00

61 lines
1.4 KiB
JavaScript

import { completeFromList } from '@codemirror/autocomplete'
import {
javascript,
javascriptLanguage,
localCompletionSource,
scopeCompletionSource,
snippets
} from '@codemirror/lang-javascript'
import { nodeJsScope } from './nodeScope.js'
function extraScope(autoCompleteObject) {
let data = autoCompleteObject
if (typeof data === 'string') {
try {
data = JSON.parse(data)
} catch {
data = {}
}
}
if (!data || typeof data !== 'object' || Array.isArray(data)) {
return {}
}
return data
}
export function nodeScopeCompletionSource(autoCompleteObject) {
return scopeCompletionSource({
...nodeJsScope,
...extraScope(autoCompleteObject)
})
}
/**
* Completions for mixed languages that mount `javascriptLanguage`
* without `javascript()` (snippets + locals + Node globals).
*/
export function javascriptCompletionSources(autoCompleteObject) {
return [
completeFromList(snippets),
localCompletionSource,
nodeScopeCompletionSource(autoCompleteObject)
]
}
/**
* JavaScript language support with Node.js globals, snippets, and
* local-variable completion.
* @param {{ autoCompleteObject?: object|string }} [options]
*/
export function javascriptLang(options = {}) {
const { autoCompleteObject } = options
return [
javascript(),
javascriptLanguage.data.of({
autocomplete: nodeScopeCompletionSource(autoCompleteObject)
})
]
}
export { nodeJsScope }