|
| 1 | +import { LiquidRawTag, NodeTypes } from '@shopify/liquid-html-parser'; |
| 2 | +import { Mode, Modes, SourceCodeType, findCurrentNode } from '@shopify/theme-check-common'; |
| 3 | +import { LanguageService, Stylesheet, getCSSLanguageService } from 'vscode-css-languageservice'; |
| 4 | +import { |
| 5 | + CompletionItem, |
| 6 | + CompletionList, |
| 7 | + CompletionParams, |
| 8 | + Diagnostic, |
| 9 | + DocumentDiagnosticParams, |
| 10 | + Hover, |
| 11 | + HoverParams, |
| 12 | + ClientCapabilities as LSPClientCapabilities, |
| 13 | +} from 'vscode-languageserver'; |
| 14 | +import { TextDocument } from 'vscode-languageserver-textdocument'; |
| 15 | +import { DocumentManager } from '../documents'; |
| 16 | + |
| 17 | +export class CSSLanguageService { |
| 18 | + private service: LanguageService | null = null; |
| 19 | + |
| 20 | + constructor(private documentManager: DocumentManager) {} |
| 21 | + |
| 22 | + async setup(clientCapabilities: LSPClientCapabilities) { |
| 23 | + this.service = getCSSLanguageService({ |
| 24 | + clientCapabilities, |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + async completions(params: CompletionParams): Promise<null | CompletionList | CompletionItem[]> { |
| 29 | + const service = this.service; |
| 30 | + if (!service) return null; |
| 31 | + const documents = this.getDocuments(params, service); |
| 32 | + if (!documents) return null; |
| 33 | + const [stylesheetTextDocument, stylesheetDocument] = documents; |
| 34 | + return service.doComplete(stylesheetTextDocument, params.position, stylesheetDocument); |
| 35 | + } |
| 36 | + |
| 37 | + async diagnostics(params: DocumentDiagnosticParams): Promise<Diagnostic[]> { |
| 38 | + const service = this.service; |
| 39 | + if (!service) return []; |
| 40 | + const documents = this.getDocuments(params, service); |
| 41 | + if (!documents) return []; |
| 42 | + const [stylesheetTextDocument, stylesheetDocument] = documents; |
| 43 | + return service.doValidation(stylesheetTextDocument, stylesheetDocument); |
| 44 | + } |
| 45 | + |
| 46 | + async hover(params: HoverParams): Promise<Hover | null> { |
| 47 | + const service = this.service; |
| 48 | + if (!service) return null; |
| 49 | + const documents = this.getDocuments(params, service); |
| 50 | + if (!documents) return null; |
| 51 | + const [stylesheetTextDocument, stylesheetDocument] = documents; |
| 52 | + return service.doHover(stylesheetTextDocument, params.position, stylesheetDocument); |
| 53 | + } |
| 54 | + |
| 55 | + private getDocuments( |
| 56 | + params: HoverParams | CompletionParams | DocumentDiagnosticParams, |
| 57 | + service: LanguageService, |
| 58 | + ): [TextDocument, Stylesheet] | null { |
| 59 | + const document = this.documentManager.get(params.textDocument.uri); |
| 60 | + if (!document) return null; |
| 61 | + |
| 62 | + switch (document.type) { |
| 63 | + case SourceCodeType.JSON: { |
| 64 | + return null; |
| 65 | + } |
| 66 | + case SourceCodeType.LiquidHtml: { |
| 67 | + if (document.ast instanceof Error) return null; |
| 68 | + const textDocument = document.textDocument; |
| 69 | + let offset = 0; |
| 70 | + let isDiagnostics = false; |
| 71 | + if ('position' in params && params.position.line !== 0 && params.position.character !== 0) { |
| 72 | + offset = textDocument.offsetAt(params.position); |
| 73 | + } else { |
| 74 | + const stylesheetIndex = document.source.indexOf('{% stylesheet %}'); |
| 75 | + offset = stylesheetIndex; |
| 76 | + isDiagnostics = true; |
| 77 | + } |
| 78 | + const [node, ancestors] = findCurrentNode(document.ast, offset); |
| 79 | + let stylesheetTag = [...ancestors].find( |
| 80 | + (node): node is LiquidRawTag => |
| 81 | + node.type === NodeTypes.LiquidRawTag && node.name === 'stylesheet', |
| 82 | + ); |
| 83 | + if (isDiagnostics && 'children' in node && node.children) { |
| 84 | + stylesheetTag = node.children.find( |
| 85 | + (node): node is LiquidRawTag => |
| 86 | + node.type === NodeTypes.LiquidRawTag && node.name === 'stylesheet', |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + if (!stylesheetTag) return null; |
| 91 | + |
| 92 | + const schemaLineNumber = textDocument.positionAt(stylesheetTag.blockStartPosition.end).line; |
| 93 | + // Hacking away "same line numbers" here by prefixing the file with newlines |
| 94 | + // This way params.position will be at the same line number in this fake jsonTextDocument |
| 95 | + // Which means that the completions will be at the same line number in the Liquid document |
| 96 | + const stylesheetString = |
| 97 | + Array(schemaLineNumber).fill('\n').join('') + |
| 98 | + stylesheetTag.source |
| 99 | + .slice(stylesheetTag.blockStartPosition.end, stylesheetTag.blockEndPosition.start) |
| 100 | + .replace(/\n$/, ''); // Remove trailing newline so parsing errors don't show up on `{% endstylesheet %}` |
| 101 | + const stylesheetTextDocument = TextDocument.create( |
| 102 | + textDocument.uri, |
| 103 | + 'json', |
| 104 | + textDocument.version, |
| 105 | + stylesheetString, |
| 106 | + ); |
| 107 | + const stylesheetDocument = service.parseStylesheet(stylesheetTextDocument); |
| 108 | + return [stylesheetTextDocument, stylesheetDocument]; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments