|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +import { TextDocument, CompletionList, CompletionItemKind, CompletionItem, TextEdit, Range, Position } from 'vscode-languageserver-types'; |
| 8 | +import { Proposed } from 'vscode-languageserver-protocol'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as fs from 'fs'; |
| 11 | +import URI from 'vscode-uri'; |
| 12 | +import { ICompletionParticipant } from 'vscode-css-languageservice/lib/umd/cssLanguageService'; |
| 13 | +import { startsWith } from './utils/strings'; |
| 14 | + |
| 15 | +export function getPathCompletionParticipant( |
| 16 | + document: TextDocument, |
| 17 | + workspaceFolders: Proposed.WorkspaceFolder[] | undefined, |
| 18 | + result: CompletionList |
| 19 | +): ICompletionParticipant { |
| 20 | + return { |
| 21 | + onCssURILiteralValue: (context: { uriValue: string, position: Position, range: Range; }) => { |
| 22 | + if (!workspaceFolders || workspaceFolders.length === 0) { |
| 23 | + return; |
| 24 | + } |
| 25 | + const workspaceRoot = resolveWorkspaceRoot(document, workspaceFolders); |
| 26 | + |
| 27 | + const suggestions = providePathSuggestions(context.uriValue, context.range, URI.parse(document.uri).fsPath, workspaceRoot); |
| 28 | + result.items = [...suggestions, ...result.items]; |
| 29 | + } |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +export function providePathSuggestions(value: string, range: Range, activeDocFsPath: string, root?: string): CompletionItem[] { |
| 34 | + if (startsWith(value, '/') && !root) { |
| 35 | + return []; |
| 36 | + } |
| 37 | + |
| 38 | + let replaceRange: Range; |
| 39 | + const lastIndexOfSlash = value.lastIndexOf('/'); |
| 40 | + if (lastIndexOfSlash === -1) { |
| 41 | + replaceRange = getFullReplaceRange(range); |
| 42 | + } else { |
| 43 | + const valueAfterLastSlash = value.slice(lastIndexOfSlash + 1); |
| 44 | + replaceRange = getReplaceRange(range, valueAfterLastSlash); |
| 45 | + } |
| 46 | + |
| 47 | + let parentDir: string; |
| 48 | + if (lastIndexOfSlash === -1) { |
| 49 | + parentDir = path.resolve(root); |
| 50 | + } else { |
| 51 | + const valueBeforeLastSlash = value.slice(0, lastIndexOfSlash + 1); |
| 52 | + |
| 53 | + parentDir = startsWith(value, '/') |
| 54 | + ? path.resolve(root, '.' + valueBeforeLastSlash) |
| 55 | + : path.resolve(activeDocFsPath, '..', valueBeforeLastSlash); |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + return fs.readdirSync(parentDir).map(f => { |
| 60 | + if (isDir(path.resolve(parentDir, f))) { |
| 61 | + return { |
| 62 | + label: f + '/', |
| 63 | + kind: CompletionItemKind.Folder, |
| 64 | + textEdit: TextEdit.replace(replaceRange, f + '/'), |
| 65 | + command: { |
| 66 | + title: 'Suggest', |
| 67 | + command: 'editor.action.triggerSuggest' |
| 68 | + } |
| 69 | + }; |
| 70 | + } else { |
| 71 | + return { |
| 72 | + label: f, |
| 73 | + kind: CompletionItemKind.File, |
| 74 | + textEdit: TextEdit.replace(replaceRange, f) |
| 75 | + }; |
| 76 | + } |
| 77 | + }); |
| 78 | + } catch (e) { |
| 79 | + return []; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +const isDir = (p: string) => { |
| 84 | + return fs.statSync(p).isDirectory(); |
| 85 | +}; |
| 86 | + |
| 87 | +function resolveWorkspaceRoot(activeDoc: TextDocument, workspaceFolders: Proposed.WorkspaceFolder[]): string | undefined { |
| 88 | + for (let i = 0; i < workspaceFolders.length; i++) { |
| 89 | + if (startsWith(activeDoc.uri, workspaceFolders[i].uri)) { |
| 90 | + return path.resolve(URI.parse(workspaceFolders[i].uri).fsPath); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function getFullReplaceRange(valueRange: Range) { |
| 96 | + const start = Position.create(valueRange.end.line, valueRange.start.character); |
| 97 | + const end = Position.create(valueRange.end.line, valueRange.end.character); |
| 98 | + return Range.create(start, end); |
| 99 | +} |
| 100 | +function getReplaceRange(valueRange: Range, valueAfterLastSlash: string) { |
| 101 | + const start = Position.create(valueRange.end.line, valueRange.end.character - valueAfterLastSlash.length); |
| 102 | + const end = Position.create(valueRange.end.line, valueRange.end.character); |
| 103 | + return Range.create(start, end); |
| 104 | +} |
0 commit comments