53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { CompletionItem, CompletionItemKind } from 'vscode-languageserver/node'
|
|
import { TextDocument } from 'vscode-languageserver-textdocument'
|
|
import { completions } from '../metadata/prelude-completions'
|
|
import { analyzeCompletionContext } from './contextAnalyzer'
|
|
|
|
/**
|
|
* Provides context-aware completions for Shrimp code.
|
|
* Returns module function completions (dict.*, list.*, str.*) or dollar property
|
|
* completions ($.*) based on the cursor position.
|
|
*/
|
|
export const provideCompletions = (
|
|
document: TextDocument,
|
|
position: { line: number; character: number },
|
|
): CompletionItem[] => {
|
|
const context = analyzeCompletionContext(document, position)
|
|
|
|
if (context.type === 'module') {
|
|
return buildModuleCompletions(context.moduleName)
|
|
}
|
|
|
|
if (context.type === 'dollar') {
|
|
return buildDollarCompletions()
|
|
}
|
|
|
|
return [] // No completions for other contexts yet
|
|
}
|
|
|
|
/**
|
|
* Builds completion items for module functions (dict.*, list.*, str.*).
|
|
*/
|
|
const buildModuleCompletions = (moduleName: string): CompletionItem[] => {
|
|
const functions = completions.modules[moduleName as keyof typeof completions.modules]
|
|
if (!functions) return []
|
|
|
|
return Object.entries(functions).map(([name, meta]) => ({
|
|
label: name,
|
|
kind: CompletionItemKind.Method,
|
|
detail: `(${meta.params.join(', ')})`,
|
|
insertText: name,
|
|
}))
|
|
}
|
|
|
|
/**
|
|
* Builds completion items for dollar properties ($.*).
|
|
*/
|
|
const buildDollarCompletions = (): CompletionItem[] => {
|
|
return Object.entries(completions.dollar).map(([name, meta]) => ({
|
|
label: name,
|
|
kind: CompletionItemKind.Property,
|
|
insertText: name,
|
|
}))
|
|
}
|