|
1 | 1 | import { CssBlockError, Syntax } from "@css-blocks/core/dist/src";
|
2 | 2 | import { BlockParser } from "@css-blocks/core/dist/src/BlockParser/BlockParser";
|
| 3 | +import * as fs from "fs"; |
| 4 | +import * as glob from "glob"; |
3 | 5 | import { postcss } from "opticss";
|
4 | 6 | import * as path from "path";
|
| 7 | +import { CompletionItem, CompletionItemKind, Position, TextDocument } from "vscode-languageserver"; |
| 8 | +import { URI } from "vscode-uri"; |
| 9 | + |
| 10 | +import { LINK_REGEX } from "../documentLinksProviders/blockLinkProvider"; |
5 | 11 |
|
6 | 12 | // TODO: Currently we are only supporting css. This should eventually support all
|
7 | 13 | // of the file types supported by css blocks
|
@@ -29,3 +35,79 @@ export async function parseBlockErrors(parser: BlockParser, blockFsPath: string,
|
29 | 35 |
|
30 | 36 | return errors;
|
31 | 37 | }
|
| 38 | + |
| 39 | +/** |
| 40 | + * If the cursor line has an import path, we check to see if the current position |
| 41 | + * of the cursor in the line is within the bounds of the import path to decide |
| 42 | + * whether to provide import path completions. |
| 43 | + */ |
| 44 | +function shouldCompleteImportPath(importPathMatches: RegExpMatchArray, position: Position, lineText: string): boolean { |
| 45 | + let relativeImportPath = importPathMatches[2]; |
| 46 | + let relativeImportPathStartLinePosition = lineText.indexOf(relativeImportPath); |
| 47 | + let relativeImportPathEndLinePosition = relativeImportPathStartLinePosition + relativeImportPath.length; |
| 48 | + return relativeImportPathStartLinePosition <= position.character && relativeImportPathEndLinePosition >= position.character; |
| 49 | +} |
| 50 | + |
| 51 | +async function getImportPathCompletions(documentUri: string, relativeImportPath: string): Promise<CompletionItem[]> { |
| 52 | + let completionItems: CompletionItem[] = []; |
| 53 | + |
| 54 | + // if the user has only typed leading dots, don't complete anything. |
| 55 | + if (/^\.+$/.test(relativeImportPath)) { |
| 56 | + return completionItems; |
| 57 | + } |
| 58 | + |
| 59 | + let blockDirPath = path.dirname(URI.parse(documentUri).fsPath); |
| 60 | + let absoluteImportPath = path.resolve(blockDirPath, relativeImportPath); |
| 61 | + let globPatternSuffix = relativeImportPath.endsWith("/") ? "/*" : "*"; |
| 62 | + let blockSyntax = path.extname(documentUri); |
| 63 | + |
| 64 | + return new Promise(outerResolve => { |
| 65 | + glob(`${absoluteImportPath}${globPatternSuffix}`, async (_, pathNames) => { |
| 66 | + let items: (CompletionItem | null)[] = await Promise.all(pathNames.map(pathName => { |
| 67 | + return new Promise(innerResolve => { |
| 68 | + fs.stat(pathName, (_, stats) => { |
| 69 | + let completionKind: CompletionItemKind | undefined; |
| 70 | + |
| 71 | + if (stats.isDirectory()) { |
| 72 | + completionKind = CompletionItemKind.Folder; |
| 73 | + } else if (stats.isFile() && path.extname(pathName) === blockSyntax) { |
| 74 | + completionKind = CompletionItemKind.File; |
| 75 | + } |
| 76 | + |
| 77 | + if (!completionKind) { |
| 78 | + innerResolve(null); |
| 79 | + } |
| 80 | + |
| 81 | + innerResolve({ |
| 82 | + label: path.basename(pathName), |
| 83 | + kind: completionKind, |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + })); |
| 88 | + |
| 89 | + // NOTE: it seems typescript is not happy with items.filter(Boolean) |
| 90 | + items.forEach(item => { |
| 91 | + if (item) { |
| 92 | + completionItems.push(item); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + outerResolve(completionItems); |
| 97 | + }); |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +// TODO: handle other completion cases (extending imported block, etc); |
| 102 | +export async function getBlockCompletions(document: TextDocument, position: Position): Promise<CompletionItem[]> { |
| 103 | + let text = document.getText(); |
| 104 | + let lineAtCursor = text.split(/\r?\n/)[position.line]; |
| 105 | + let importPathMatches = lineAtCursor.match(LINK_REGEX); |
| 106 | + |
| 107 | + if (importPathMatches && shouldCompleteImportPath(importPathMatches, position, lineAtCursor)) { |
| 108 | + let relativeImportPath = importPathMatches[2]; |
| 109 | + return await getImportPathCompletions(document.uri, relativeImportPath); |
| 110 | + } |
| 111 | + |
| 112 | + return []; |
| 113 | +} |
0 commit comments