|
| 1 | +import { LiquidRawTag, NodeTypes } from '@shopify/liquid-html-parser'; |
| 2 | +import { AbstractFileSystem, Mode, Modes, SourceCodeType, findCurrentNode } from '@shopify/theme-check-common'; |
| 3 | +import ts, { LanguageService } from 'typescript'; |
| 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 | +import { findJSConfig } from './utils'; |
| 17 | + |
| 18 | +export class JSLanguageService { |
| 19 | + private service: LanguageService | null = null; |
| 20 | + private snapshotManager: Map<string, ts.IScriptSnapshot> = new Map(); |
| 21 | + |
| 22 | + constructor(private documentManager: DocumentManager) {} |
| 23 | + |
| 24 | + async setup(clientCapabilities: LSPClientCapabilities, fs: AbstractFileSystem, workspacePath: string) { |
| 25 | + // TODO: we need to get fileExists to be synchronous |
| 26 | + const configPath = findJSConfig(workspacePath, [], fs.exists); |
| 27 | + const projectConfig = JSON.parse(await fs.readFile(configPath)); |
| 28 | + const { compilerOptions } = projectConfig; |
| 29 | + let compilerHost: ts.CompilerHost | null = null; |
| 30 | + const host: ts.LanguageServiceHost = { |
| 31 | + getCompilationSettings: () => compilerOptions, |
| 32 | + getScriptFileNames: () => [], |
| 33 | + // TODO: we need to get fileExists, readFile and readDirectory to be synchronous |
| 34 | + fileExists: fs.exists, |
| 35 | + readFile: (uri) => fs.readFile(uri), |
| 36 | + // TODO: we need a snapshot manager |
| 37 | + getScriptVersion: (fileName: string) => '1', |
| 38 | + getScriptSnapshot: (fileName: string) => |
| 39 | + this.snapshotManager.get(fileName) || ts.ScriptSnapshot.fromString(''), |
| 40 | + getCurrentDirectory: () => workspacePath, |
| 41 | + getDefaultLibFileName: ts.getDefaultLibFilePath, |
| 42 | + useSourceOfProjectReferenceRedirect() { |
| 43 | + return false; |
| 44 | + }, |
| 45 | + setCompilerHost: (host: ts.CompilerHost) => (compilerHost = host), |
| 46 | + getCompilerHost: () => compilerHost! |
| 47 | + }; |
| 48 | + this.service = ts.createLanguageService(host); |
| 49 | + } |
| 50 | + |
| 51 | + async completions(params: CompletionParams): Promise<null | CompletionList | CompletionItem[]> { |
| 52 | + const service = this.service; |
| 53 | + if (!service) return null; |
| 54 | + const jsDocument = this.getDocuments(params, service); |
| 55 | + if (!jsDocument) return null; |
| 56 | + // TODO: convert position to offset |
| 57 | + const completions = await service.getCompletionsAtPosition(jsDocument.uri, positionToOffset(jsDocument, params.position), {}); |
| 58 | + if (!completions) return null; |
| 59 | + return completions; |
| 60 | + } |
| 61 | + |
| 62 | + async diagnostics(params: DocumentDiagnosticParams): Promise<Diagnostic[]> { |
| 63 | + const service = this.service; |
| 64 | + if (!service) return []; |
| 65 | + const jsDocument = this.getDocuments(params, service); |
| 66 | + if (!jsDocument) return []; |
| 67 | + return service.getSemanticDiagnostics(jsDocument.uri); |
| 68 | + } |
| 69 | + |
| 70 | + async hover(params: HoverParams): Promise<Hover | null> { |
| 71 | + const service = this.service; |
| 72 | + if (!service) return null; |
| 73 | + const jsDocument = this.getDocuments(params, service); |
| 74 | + if (!jsDocument) return null; |
| 75 | + const hover = await service.getQuickInfoAtPosition(jsDocument.uri, positionToOffset(jsDocument, params.position)); |
| 76 | + if (!hover) return null; |
| 77 | + return hover; |
| 78 | + } |
| 79 | + |
| 80 | + private getDocuments( |
| 81 | + params: HoverParams | CompletionParams | DocumentDiagnosticParams, |
| 82 | + service: LanguageService, |
| 83 | + ): TextDocument | null { |
| 84 | + const document = this.documentManager.get(params.textDocument.uri); |
| 85 | + if (!document) return null; |
| 86 | + |
| 87 | + switch (document.type) { |
| 88 | + case SourceCodeType.JSON: { |
| 89 | + return null; |
| 90 | + } |
| 91 | + case SourceCodeType.LiquidHtml: { |
| 92 | + if (document.ast instanceof Error) return null; |
| 93 | + const textDocument = document.textDocument; |
| 94 | + let offset = 0; |
| 95 | + let isDiagnostics = false; |
| 96 | + if ('position' in params && params.position.line !== 0 && params.position.character !== 0) { |
| 97 | + offset = textDocument.offsetAt(params.position); |
| 98 | + } else { |
| 99 | + const stylesheetIndex = document.source.indexOf('{% javascript %}'); |
| 100 | + offset = stylesheetIndex; |
| 101 | + isDiagnostics = true; |
| 102 | + } |
| 103 | + const [node, ancestors] = findCurrentNode(document.ast, offset); |
| 104 | + let stylesheetTag = [...ancestors].find( |
| 105 | + (node): node is LiquidRawTag => |
| 106 | + node.type === NodeTypes.LiquidRawTag && node.name === 'javascript', |
| 107 | + ); |
| 108 | + if (isDiagnostics && 'children' in node && node.children) { |
| 109 | + stylesheetTag = node.children.find( |
| 110 | + (node): node is LiquidRawTag => |
| 111 | + node.type === NodeTypes.LiquidRawTag && node.name === 'stylesheet', |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + if (!stylesheetTag) return null; |
| 116 | + |
| 117 | + const schemaLineNumber = textDocument.positionAt(stylesheetTag.blockStartPosition.end).line; |
| 118 | + // Hacking away "same line numbers" here by prefixing the file with newlines |
| 119 | + // This way params.position will be at the same line number in this fake jsonTextDocument |
| 120 | + // Which means that the completions will be at the same line number in the Liquid document |
| 121 | + const stylesheetString = |
| 122 | + Array(schemaLineNumber).fill('\n').join('') + |
| 123 | + stylesheetTag.source |
| 124 | + .slice(stylesheetTag.blockStartPosition.end, stylesheetTag.blockEndPosition.start) |
| 125 | + .replace(/\n$/, ''); // Remove trailing newline so parsing errors don't show up on `{% endstylesheet %}` |
| 126 | + const stylesheetTextDocument = TextDocument.create( |
| 127 | + textDocument.uri, |
| 128 | + 'json', |
| 129 | + textDocument.version, |
| 130 | + stylesheetString, |
| 131 | + ); |
| 132 | + return stylesheetTextDocument |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function positionToOffset(textDocument: TextDocument, position: Position): number { |
| 139 | + return textDocument.offsetAt(position); |
| 140 | +} |
0 commit comments