From 80289b82b876f3e9ef52d196a75a67c034b62f31 Mon Sep 17 00:00:00 2001 From: Peter van Gulik Date: Sat, 16 Dec 2023 22:57:38 +0100 Subject: [PATCH] add new code lens and grammar for SF APIs --- packages/vscode-extension/package.json | 16 +++++++++ .../executeApiLensProvider.ts | 36 +++++++++++++++++++ packages/vscode-extension/src/extension.ts | 7 ++-- .../symbolProviders/apexLogSymbolProvider.ts | 21 ++++++----- .../syntax/sfhttp.tmLanguage.json | 36 +++++++++++++++++++ pnpm-lock.yaml | 33 ++++++++++++++--- 6 files changed, 131 insertions(+), 18 deletions(-) create mode 100644 packages/vscode-extension/src/codeLensProviders/executeApiLensProvider.ts create mode 100644 packages/vscode-extension/syntax/sfhttp.tmLanguage.json diff --git a/packages/vscode-extension/package.json b/packages/vscode-extension/package.json index 6d201b9e..ff0d6186 100644 --- a/packages/vscode-extension/package.json +++ b/packages/vscode-extension/package.json @@ -1275,6 +1275,11 @@ "language": "vldp", "scopeName": "source.json", "path": "./syntax/datapack.tmLanguage.json" + }, + { + "language": "sfhttp", + "scopeName": "source.sfhttp", + "path": "./syntax/sfhttp.tmLanguage.json" } ], "languages": [ @@ -1290,6 +1295,16 @@ ], "configuration": "./syntax/datapack-language.json" }, + { + "id": "sfhttp", + "aliases": [ + "Salesforce HTTP API" + ], + "extensions": [ + ".sfhttp", + ".http" + ] + }, { "id": "xml", "extensions": [ @@ -1578,6 +1593,7 @@ "vsce": "^2.15.0", "vscode-languageclient": "^8.1.0", "vscode-test": "^1.6.1", + "vscode-tmgrammar-test": "^0.1.2", "webpack": "^5.88.2", "webpack-cli": "^5.1.4", "webpack-glob-entry": "^2.1.1", diff --git a/packages/vscode-extension/src/codeLensProviders/executeApiLensProvider.ts b/packages/vscode-extension/src/codeLensProviders/executeApiLensProvider.ts new file mode 100644 index 00000000..0a5d9793 --- /dev/null +++ b/packages/vscode-extension/src/codeLensProviders/executeApiLensProvider.ts @@ -0,0 +1,36 @@ +import * as vscode from 'vscode'; + +import VlocodeService from "../lib/vlocodeService"; +import { container, injectable } from "@vlocode/core"; + +@injectable() +export class ExecuteApiLensProvider implements vscode.CodeLensProvider { + + private regex = /^(GET|POST|PUT|DELETE|PATCH) (.*)$/i + + public static register(service: VlocodeService) { + const lens = container.get(ExecuteApiLensProvider); + vscode.languages.registerCodeLensProvider({ pattern: '**/*.{api,http,sfhttp,sfapi}' }, lens); + vscode.languages.registerCodeLensProvider({ language: 'sfhttp' }, lens); + } + + public provideCodeLenses(document: vscode.TextDocument): vscode.CodeLens[] { + for (let i = 0; i < document.lineCount; i++) { + const line = document.lineAt(i); + const match = line.text.match(this.regex); + if (match) { + return [ + new vscode.CodeLens(line.range, { + title: `Execute as Salesforce API Request (${match[1].toLowerCase()})`, + tooltip: "Execute the API request described in this file on the selected Salesforce org", + command: "vlocode.api.execute", + arguments: [ document ] + }) + ]; + } else if (line.text) { + break; + } + } + return []; + } +} \ No newline at end of file diff --git a/packages/vscode-extension/src/extension.ts b/packages/vscode-extension/src/extension.ts index 3f16cddb..7145804d 100644 --- a/packages/vscode-extension/src/extension.ts +++ b/packages/vscode-extension/src/extension.ts @@ -31,6 +31,7 @@ import { VlocityNamespaceService } from '@vlocode/vlocity'; import { SfdxConfigWatcher } from './lib/sfdxConfigWatcher'; import './commands'; +import { ExecuteApiLensProvider } from './codeLensProviders/executeApiLensProvider'; /** * Start time of the extension set when the extension is packed by webpack when the entry point is loaded @@ -205,16 +206,18 @@ class Vlocode { // Add apex LOG symbol provider try { - this.service.registerDisposable(vscode.languages.registerDocumentSymbolProvider({ language: 'apexlog' }, new ApexLogSymbolProvider())); + ApexLogSymbolProvider.register(this.service); } catch(err) { this.logger.warn(`Unable to register symbol provider for APEX logs: ${err}`); } + ExecuteApiLensProvider.register(this.service); + // Watch conditionalContextMenus for changes ConfigurationManager.onConfigChange(this.service.config, 'conditionalContextMenus', config => vscode.commands.executeCommand('setContext', `${constants.CONTEXT_PREFIX}.conditionalContextMenus`, config.conditionalContextMenus), { initial: true }); - // watch for changes + // watch for changes void this.service.registerDisposable(container.create(WorkspaceContextDetector, 'datapacks', DatapackDetector.filter()).initialize()); void this.service.registerDisposable(container.create(WorkspaceContextDetector, 'metadata', MetadataDetector.filter()).initialize()); void this.service.registerDisposable(container.create(SfdxConfigWatcher).initialize()); diff --git a/packages/vscode-extension/src/symbolProviders/apexLogSymbolProvider.ts b/packages/vscode-extension/src/symbolProviders/apexLogSymbolProvider.ts index 5492346a..446866c8 100644 --- a/packages/vscode-extension/src/symbolProviders/apexLogSymbolProvider.ts +++ b/packages/vscode-extension/src/symbolProviders/apexLogSymbolProvider.ts @@ -1,25 +1,24 @@ import * as vscode from 'vscode'; -/** - * -09:41:19.369 (369138479)|METHOD_ENTRY|[1]|01p0Y00000O9SLP|OrderRepository.OrderRepository() -09:41:19.369 (369244047)|METHOD_EXIT|[1]|OrderRepository -09:41:19.369 (369309631)|CONSTRUCTOR_ENTRY|[17]|01p0Y00000O9SLP|()|OrderRepository -09:41:19.369 (369582949)|CONSTRUCTOR_EXIT|[17]|01p0Y00000O9SLP|()|OrderRepository -09:41:19.369 (369616240)|CONSTRUCTOR_ENTRY|[17]|01p5E000001BvvA|(IOrderRepository)|OrderItemsController -09:41:19.369 (369653962)|METHOD_ENTRY|[4]|01p5E000001FJwa|VlocityController.VlocityController() -09:41:19.369 (369667080)|METHOD_EXIT|[4]|VlocityController -09:41:19.369 (369616240)|CONSTRUCTOR_EXIT|[17]|01p5E000001BvvA|(IOrderRepository)|OrderItemsController - */ +import { container, injectable } from '@vlocode/core'; +import VlocodeService from '../lib/vlocodeService'; /** * Provides Symbol and Outline information for Salesforce APEX logs */ +@injectable() export class ApexLogSymbolProvider implements vscode.DocumentSymbolProvider { readonly functionPattern = /^(?