microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/debuggingConfiguration/launchJsonCompletionProvider.ts
65lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as vscode from "vscode"; |
| 5 | import * as path from "path"; |
| 6 | import { getLocation } from "jsonc-parser"; |
| 7 | import * as nls from "vscode-nls"; |
| 8 | nls.config({ |
| 9 | messageFormat: nls.MessageFormat.bundle, |
| 10 | bundleFormat: nls.BundleFormat.standalone, |
| 11 | })(); |
| 12 | const localize = nls.loadMessageBundle(); |
| 13 | |
| 14 | export enum JsonLanguages { |
| 15 | json = "json", |
| 16 | jsonWithComments = "jsonc", |
| 17 | } |
| 18 | |
| 19 | export class LaunchJsonCompletionProvider implements vscode.CompletionItemProvider { |
| 20 | private readonly configurationNodeName = "configurations"; |
| 21 | |
| 22 | public provideCompletionItems( |
| 23 | document: vscode.TextDocument, |
| 24 | position: vscode.Position, |
| 25 | token: vscode.CancellationToken, |
| 26 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 27 | context: vscode.CompletionContext, |
| 28 | ): vscode.ProviderResult<vscode.CompletionItem[] | vscode.CompletionList> { |
| 29 | if (!this.canProvideCompletions(document, position)) { |
| 30 | return []; |
| 31 | } |
| 32 | |
| 33 | return [ |
| 34 | { |
| 35 | command: { |
| 36 | command: "reactNative.selectAndInsertDebugConfiguration", |
| 37 | title: localize( |
| 38 | "SelectReactNativeDebugConfiguration", |
| 39 | "Select a React Native debug configuration", |
| 40 | ), |
| 41 | arguments: [document, position, token], |
| 42 | }, |
| 43 | documentation: localize( |
| 44 | "SelectReactNativeDebugConfiguration", |
| 45 | "Select a React Native debug configuration", |
| 46 | ), |
| 47 | sortText: "AAAA", |
| 48 | preselect: true, |
| 49 | kind: vscode.CompletionItemKind.Enum, |
| 50 | label: "React Native", |
| 51 | insertText: new vscode.SnippetString(), |
| 52 | }, |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | private canProvideCompletions(document: vscode.TextDocument, position: vscode.Position) { |
| 57 | if (path.basename(document.uri.fsPath) !== "launch.json") { |
| 58 | return false; |
| 59 | } |
| 60 | const location = getLocation(document.getText(), document.offsetAt(position)); |
| 61 | // Cursor must be inside the configurations array and not in any nested items. |
| 62 | // Hence path[0] = array, path[1] = array element index. |
| 63 | return location.path[0] === this.configurationNodeName && location.path.length === 2; |
| 64 | } |
| 65 | } |
| 66 | |