|
| 1 | +import { CompletionItem, TextDocument } from 'vscode-languageserver'; |
| 2 | +import { LiquidCompletionParams } from '../params'; |
| 3 | +import { Provider } from './common'; |
| 4 | +import { getCssLanguageService } from '../../plugins/css'; |
| 5 | +import { Position } from 'vscode-json-languageservice'; |
| 6 | + |
| 7 | +export class CompletionsForStyleSheetProvider implements Provider { |
| 8 | + constructor( |
| 9 | + private readonly getThemeBlockNames: ( |
| 10 | + rootUri: string, |
| 11 | + includePrivate: boolean, |
| 12 | + ) => Promise<string[]>, |
| 13 | + ) {} |
| 14 | + |
| 15 | + async completions(params: LiquidCompletionParams): Promise<CompletionItem[]> { |
| 16 | + if (!params.completionContext) return []; |
| 17 | + |
| 18 | + const { node } = params.completionContext; |
| 19 | + const { document } = params; |
| 20 | + if (node && document.stylesheet && document.stylesheet.cssStart >= node.position.start && document.stylesheet.cssEnd <= node.position.end) { |
| 21 | + const cssLanguageService = getCssLanguageService('css'); |
| 22 | + const offset = document.stylesheet.cssStart; |
| 23 | + const position = offsetToPosition(document.stylesheet.source, offset); |
| 24 | + const stylesheet = cssLanguageService.parseStylesheet(document.stylesheet.source); |
| 25 | + const completions = cssLanguageService.doComplete(document.stylesheet.source, position, stylesheet, {triggerPropertyValueCompletion: true }); |
| 26 | + return completions.items.map(item => ({ |
| 27 | + label: item.label, |
| 28 | + kind: item.kind, |
| 29 | + insertText: item.insertText, |
| 30 | + documentation: item.documentation, |
| 31 | + detail: item.detail, |
| 32 | + })); |
| 33 | + } else { |
| 34 | + return []; |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function offsetToPosition(source: TextDocument, offset: number): Position { |
| 40 | + const line = source.getText().slice(0, offset).split('\n').length - 1; |
| 41 | + const character = offset - source.getText().slice(0, offset).lastIndexOf('\n'); |
| 42 | + return { line, character }; |
| 43 | +} |
0 commit comments