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