Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.

Commit d60f35f

Browse files
authored
FUSETOOLS2-1067 - adding settings and doc (#530)
* FUSETOOLS2-1067 - adding settings and doc * updating available commands doc * adding tests * handling blank value Signed-off-by: Brian Fitzpatrick <bfitzpat@redhat.com>
1 parent b9335e1 commit d60f35f

10 files changed

Lines changed: 152 additions & 19 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ All notable changes to the "vscode-didact" extension will be documented in this
2323
- Added `Insert Didact Badge` completion to show `Powered by Didact` badge with link back to the project in GitHub
2424
- Added new `vscode` URI processing to handle registering a Didact tutorial via a web link and a new command - `Didact: Process VSCode link from web` to process a copied link
2525
- Add new `Ctrl/Cmd+Alt+T` shortcut to convert selected text into a `sendNamedTerminalAString` Didact link
26+
- Added new settings to customize the link text shown and whether to execute the command in the terminal
27+
- Add new `sendNamedTerminalAStringNoLF` command that sends text to the terminal but doesn't execute it
2628

2729
## 0.3.2
2830

docs/available-commands.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ Sends text to a named terminal, which is then executed in the terminal. If the n
8181

8282
Note: In order to send multiple commands spread out across multiple links in the same terminal window, ensure that the `name` passed is consistent.
8383

84+
## vscode.didact.sendNamedTerminalAStringNoLF
85+
86+
Same as sendNamedTerminalAString, but the command is not executed in the terminal by default.
87+
8488
## vscode.didact.sendNamedTerminalCtrlC
8589

8690
Send a Ctrl+C key combination to a named terminal to stop a long-running process. Terminal with the given name must be accessible.
@@ -212,6 +216,18 @@ Command to copy text directly from the URL to the clipboard. Handy for short str
212216
* Input: URL encoded text to copy onto the clipboard
213217
* Example: `didact://?commandId=vscode.didact.copyToClipboardCommand&text=The%20fox%20jumped%20over%20the%20lazy%20dog.`
214218

219+
## vscode.didact.copyTextToCLI
220+
221+
Command to take the currently selected text in the open editor (of a Didact Markdown or AsciiDoc file) and add a new Didact `sendNamedTerminalAString` or `sendNamedTerminalAStringNoLF` link with the selected text automatically URLencoded.
222+
223+
This is triggered by default using `Ctrl+Alt+T` or `Cmd+Alt+T`. Note that the key combination can be changed by going into Keyboard Shortcuts (`File->Preferences->Keyboard Shortcuts`) and searching for `copyTextToCLI`, then overwriting the Keybinding.
224+
225+
A couple of things to note:
226+
227+
* Any link inserted using this method will be surrounded by parentheses. For example, `^ execute` will appear in the Didact link as `(^ execute)`.
228+
* Though you can modify the text inside the parentheses (i.e. `^ execute`) in the [`Didact>Edit: Cli Link Text` field in the Settings for Didact](https://redhat-developer.github.io/vscode-didact/settings), some characters will need to be escaped such as close square brackets (`\]`) due to the way links are formatted in Markdown and AsciiDoc.
229+
* You can insert emojis such as the [play button](https://emojipedia.org/play-button/) or [two-hump camel](https://emojipedia.org/two-hump-camel/) instead of text.
230+
215231
***
216232
# Commands Elsewhere
217233

docs/images/settings.jpg

8.92 KB
Loading

docs/settings.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ The Didact Extension has several configurable settings available. To access the
77
* To turn on generic Didact completion notifications (off by default), set the `Didact: Disable Notifications` checkbox. If unchecked, additional notifications will occur when Didact actions are activated in the IDE.
88
* To have Didact open a window with the default Didact file on startup, set the `Didact: Open Default Tutorial At Startup` checkbox.
99

10+
The `vscode.didact.copyTextToCLI` command (accessible via the default keyboard shorcut `Ctrl/Cmd+Alt+T`) has two customizations available:
11+
12+
* By default, the Didact link generated commits the change, executing it in the VS Code terminal. To simply add the text to the terminal without executing it, uncheck the `Didact>Edit:Cli Link LF` checkbox.
13+
* By default, the Didact link generated uses the label `(^ Execute)`, but you can change that by updating the `Didact>Edit: Cli Link Text` field. Note that this field can also use Emojis, so you can use characters such as the `Play` button [(▶️)](https://emojipedia.org/play-button/).
14+
1015
![Didact Settings](./images/settings.jpg)

package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"contributes": {
4646
"configuration": [
4747
{
48-
"title": "Didact Extension Settings",
48+
"title": "Didact",
4949
"properties": {
5050
"didact.defaultUrl": {
5151
"type": "string",
@@ -70,6 +70,20 @@
7070
"default": "true",
7171
"description": "Automatically add the default Didact tutorials to the Didact Tutorials view at workspace startup if not already registered",
7272
"scope": "window"
73+
},
74+
"didact.edit.cliLinkText": {
75+
"type": "string",
76+
"default": "^ execute",
77+
"markdownDescription": "Link text to use when converting text for use in a copyTextToCLI terminal link. Cannot be an empty string. Special characters must be escaped. Defaults to `^ execute`. [Read more.](https://redhat-developer.github.io/vscode-didact/settings)",
78+
"scope": "window",
79+
"pattern": "^(?!\\s*$).+",
80+
"patternErrorMessage": "Link text cannot be an empty string. Please provide at least one character."
81+
},
82+
"didact.edit.cliLinkLF": {
83+
"type": "boolean",
84+
"default": "true",
85+
"markdownDescription": "Automatically execute copyTextToCLI command in generated CLI terminal link. [Read more.](https://redhat-developer.github.io/vscode-didact/settings)",
86+
"scope": "window"
7387
}
7488
}
7589
}

src/extension.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ import { DidactNodeProvider, SimpleNode } from './nodeProvider';
2121
import { registerTutorialWithCategory, clearRegisteredTutorials, getOpenAtStartupSetting,
2222
clearOutputChannels, registerTutorialWithJSON, getAutoInstallDefaultTutorialsSetting,
2323
addNewTutorialWithNameAndCategoryForDidactUri,
24-
removeTutorialByNameAndCategory,
25-
getValue} from './utils';
24+
removeTutorialByNameAndCategory } from './utils';
2625
import { DidactUriCompletionItemProvider } from './didactUriCompletionItemProvider';
2726
import { DidactPanelSerializer } from './didactPanelSerializer';
2827
import { didactManager, VIEW_TYPE } from './didactManager';
29-
import { handleVSCodeDidactUriParsingForPath, handleVSCodeUri, sendTextToOutputChannel } from './extensionFunctions';
30-
import * as querystring from 'querystring';
28+
import { handleVSCodeUri } from './extensionFunctions';
3129

3230
const DIDACT_VIEW = 'didact.tutorials';
3331

@@ -48,6 +46,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4846
context.subscriptions.push(vscode.commands.registerCommand(extensionFunctions.START_DIDACT_COMMAND, extensionFunctions.revealOrStartDidactByURI));
4947
context.subscriptions.push(vscode.commands.registerCommand(extensionFunctions.START_TERMINAL_COMMAND, extensionFunctions.startTerminal));
5048
context.subscriptions.push(vscode.commands.registerCommand(extensionFunctions.SEND_TERMINAL_SOME_TEXT_COMMAND, extensionFunctions.sendTerminalText));
49+
context.subscriptions.push(vscode.commands.registerCommand(extensionFunctions.SEND_TERMINAL_SOME_TEXT_COMMAND_NO_LF, extensionFunctions.sendTerminalTextNoLF));
5150
context.subscriptions.push(vscode.commands.registerCommand(extensionFunctions.REQUIREMENT_CHECK_COMMAND, extensionFunctions.requirementCheck));
5251
context.subscriptions.push(vscode.commands.registerCommand(extensionFunctions.EXTENSION_REQUIREMENT_CHECK_COMMAND, extensionFunctions.extensionCheck));
5352
context.subscriptions.push(vscode.commands.registerCommand(extensionFunctions.WORKSPACE_FOLDER_EXISTS_CHECK_COMMAND, extensionFunctions.validWorkspaceCheck));

src/extensionFunctions.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ import { handleExtFilePath, handleProjectFilePath } from './commandHandler';
2828
import * as download from 'download';
2929
import { didactManager } from './didactManager';
3030
import { parse } from 'node-html-parser';
31-
import { addNewTutorialWithNameAndCategoryForDidactUri, delay, DIDACT_DEFAULT_URL, getCachedOutputChannel, getCurrentFileSelectionPath, getValue, getWorkspacePath, registerTutorialWithCategory, rememberOutputChannel } from './utils';
31+
import { addNewTutorialWithNameAndCategoryForDidactUri, delay, DIDACT_DEFAULT_URL, getCachedOutputChannel, getCurrentFileSelectionPath, getInsertLFForCLILinkSetting, getLinkTextForCLILinkSetting, getValue, getWorkspacePath, registerTutorialWithCategory, rememberOutputChannel } from './utils';
3232

3333
const tmp = require('tmp');
3434
const fetch = require('node-fetch');
3535
const url = require('url-parse');
36-
const EDITOR_OPENED_TIMEOUT = 3000;
3736

3837
// command IDs
3938
export const SCAFFOLD_PROJECT_COMMAND = 'vscode.didact.scaffoldProject';
4039
export const OPEN_TUTORIAL_COMMAND = 'vscode.didact.openTutorial';
4140
export const START_DIDACT_COMMAND = 'vscode.didact.startDidact';
4241
export const START_TERMINAL_COMMAND = 'vscode.didact.startTerminalWithName';
4342
export const SEND_TERMINAL_SOME_TEXT_COMMAND = 'vscode.didact.sendNamedTerminalAString';
43+
export const SEND_TERMINAL_SOME_TEXT_COMMAND_NO_LF = 'vscode.didact.sendNamedTerminalAStringNoLF';
4444
export const REQUIREMENT_CHECK_COMMAND = 'vscode.didact.requirementCheck';
4545
export const EXTENSION_REQUIREMENT_CHECK_COMMAND = 'vscode.didact.extensionRequirementCheck';
4646
export const WORKSPACE_FOLDER_EXISTS_CHECK_COMMAND = 'vscode.didact.workspaceFolderExistsCheck';
@@ -185,10 +185,10 @@ export async function startTerminal(...rest: any[]): Promise<void>{ //name:strin
185185
}
186186
}
187187

188-
export async function showAndSendText(terminal: vscode.Terminal, text:string): Promise<void> {
188+
export async function showAndSendText(terminal: vscode.Terminal, text:string, sendLF = true): Promise<void> {
189189
if (terminal) {
190190
terminal.show();
191-
terminal.sendText(text);
191+
terminal.sendText(text, sendLF);
192192
}
193193
}
194194

@@ -220,15 +220,24 @@ export function findTerminal(name: string) : vscode.Terminal | undefined {
220220
}
221221

222222
// send a message to a named terminal
223-
export async function sendTerminalText(name:string, text:string): Promise<void> {
223+
export async function sendTerminalText(name:string, text:string, sendLF = true): Promise<void> {
224224
let terminal : vscode.Terminal | undefined = findTerminal(name);
225225
if (!terminal) {
226226
terminal = vscode.window.createTerminal(name);
227227
}
228228
if (terminal) {
229-
showAndSendText(terminal, text);
229+
showAndSendText(terminal, text, sendLF);
230+
}
231+
const msg = `Sent terminal ${name} the text ${text}`;
232+
if (sendLF) {
233+
sendTextToOutputChannel(msg + ` with LF`);
234+
} else {
235+
sendTextToOutputChannel(msg);
230236
}
231-
sendTextToOutputChannel(`Sent terminal ${name} the text ${text}`);
237+
}
238+
239+
export async function sendTerminalTextNoLF(name:string, text:string): Promise<void> {
240+
return sendTerminalText(name, text, false);
232241
}
233242

234243
export async function sendTerminalCtrlC(name:string): Promise<void> {
@@ -1137,6 +1146,22 @@ export async function handleVSCodeUri(uri:vscode.Uri | undefined) : Promise<void
11371146
}
11381147
}
11391148

1149+
// for testing purposes
1150+
export function getDidactLinkForSelectedText(selectedText : string, isAdoc : boolean) : string {
1151+
const encodedText = encodeURI(selectedText);
1152+
const linkText = getLinkTextForCLILinkSetting();
1153+
const linkUseLF = getInsertLFForCLILinkSetting();
1154+
let commandToUse = 'vscode.didact.sendNamedTerminalAString';
1155+
if(!linkUseLF) {
1156+
commandToUse = 'vscode.didact.sendNamedTerminalAStringNoLF';
1157+
}
1158+
let templatedLink = ` ([${linkText}](didact://?commandId=${commandToUse}&text=newTerminal$$${encodedText}))`;
1159+
if (isAdoc) {
1160+
templatedLink = ` link:didact://?commandId=${commandToUse}&text=newTerminal$$${encodedText}[(${linkText})]`;
1161+
}
1162+
return templatedLink;
1163+
}
1164+
11401165
export async function convertSelectionToCLILinkAndInsertAfterSelection() : Promise<void> {
11411166
const editor = vscode.window.activeTextEditor;
11421167
if (editor) {
@@ -1147,12 +1172,8 @@ export async function convertSelectionToCLILinkAndInsertAfterSelection() : Promi
11471172
const selection = vscode.window.activeTextEditor?.selection;
11481173
const selectedText = vscode.window.activeTextEditor?.document.getText(selection);
11491174
if (selectedText && selectedText.trim().length > 0) {
1150-
const encodedText = encodeURI(selectedText);
11511175
const isAdoc = isAsciiDoc(editor.document.uri?.toString());
1152-
let templatedLink = ` ([^ execute](didact://?commandId=vscode.didact.sendNamedTerminalAString&text=newTerminal$$${encodedText}))`;
1153-
if (isAdoc) {
1154-
templatedLink = ` link:didact://?commandId=vscode.didact.sendNamedTerminalAString&text=newTerminal$$${encodedText}[(^ execute)]`;
1155-
}
1176+
const templatedLink = getDidactLinkForSelectedText(selectedText, isAdoc);
11561177
await insertTextAtFirstWhitespacePastCurrentSelection(templatedLink);
11571178
}
11581179
}

src/test/suite/didactUriCompletionItemProvider.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ suite("Didact URI completion provider tests", function () {
110110
test("that the command processing for a command prefix returns expected results", async () => {
111111
const match = provider.findMatchForCommandVariable('didact://?commandId=vscode.didact.');
112112
const completionList = await provider.processCommands(match);
113-
expect(completionList.items).to.have.lengthOf(34)
113+
expect(completionList.items).to.have.lengthOf(35)
114114
});
115115

116116
test("that the command processing for one command returns one expected result", async () => {

src/test/suite/extensionFunctions.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from 'vscode';
33
import * as extensionFunctions from '../../extensionFunctions';
44
import { handleExtFilePath, handleProjectFilePath } from '../../commandHandler';
55
import * as path from 'path';
6-
import { removeFilesAndFolders, getCachedOutputChannel, getCachedOutputChannels } from '../../utils';
6+
import { removeFilesAndFolders, getCachedOutputChannel, getCachedOutputChannels, setInsertLFForCLILinkSetting, getLinkTextForCLILinkSetting, setLinkTextForCLILinkSetting, DEFAULT_EXECUTE_LINK_TEXT } from '../../utils';
77
import { beforeEach, after, afterEach } from 'mocha';
88
import * as sinon from 'sinon';
99
import { expect } from 'chai';
@@ -317,6 +317,56 @@ suite('Extension Functions Test Suite', () => {
317317
expect.fail("Editor did not open");
318318
}
319319
});
320+
321+
test('verify inserted link text for markdown CLI and default send terminal command', async() => {
322+
const selectedText = `echo The quick brown fox markdown`;
323+
await setInsertLFForCLILinkSetting(true); // reset default
324+
const linkText = getLinkTextForCLILinkSetting();
325+
const generatedText = extensionFunctions.getDidactLinkForSelectedText(selectedText, false);
326+
const encodedText = encodeURI(selectedText);
327+
const defaultCommandToUse = extensionFunctions.SEND_TERMINAL_SOME_TEXT_COMMAND;
328+
const expectedLink = ` ([${linkText}](didact://?commandId=${defaultCommandToUse}&text=newTerminal$$${encodedText}))`;
329+
expect(generatedText.trim()).to.deep.equal(expectedLink.trim());
330+
});
331+
332+
test('verify inserted link text for adoc CLI and LF send terminal command setting set', async() => {
333+
const selectedText = `echo The quick brown fox adoc`;
334+
await setInsertLFForCLILinkSetting(false);
335+
const linkText = getLinkTextForCLILinkSetting();
336+
const generatedText = extensionFunctions.getDidactLinkForSelectedText(selectedText, true);
337+
const encodedText = encodeURI(selectedText);
338+
const defaultCommandToUse = extensionFunctions.SEND_TERMINAL_SOME_TEXT_COMMAND_NO_LF;
339+
const expectedLink = ` link:didact://?commandId=${defaultCommandToUse}&text=newTerminal$$${encodedText}[(${linkText})]`;
340+
await setInsertLFForCLILinkSetting(true); // reset default
341+
expect(generatedText.trim()).to.deep.equal(expectedLink.trim());
342+
});
343+
344+
test('verify altered link text appears in generated CLI link', async() => {
345+
const selectedText = `echo The quick brown fox`;
346+
await setInsertLFForCLILinkSetting(true); // reset default
347+
const newLinkText = `**link**`;
348+
await setLinkTextForCLILinkSetting(newLinkText);
349+
const generatedText = extensionFunctions.getDidactLinkForSelectedText(selectedText, false);
350+
const encodedText = encodeURI(selectedText);
351+
const defaultCommandToUse = extensionFunctions.SEND_TERMINAL_SOME_TEXT_COMMAND;
352+
const expectedLink = ` ([${newLinkText}](didact://?commandId=${defaultCommandToUse}&text=newTerminal$$${encodedText}))`;
353+
await setLinkTextForCLILinkSetting(DEFAULT_EXECUTE_LINK_TEXT); // reset default
354+
expect(generatedText.trim()).to.deep.equal(expectedLink.trim());
355+
});
356+
357+
test('validate that if we clear the link text setting, it resets to default', async() => {
358+
await setLinkTextForCLILinkSetting(DEFAULT_EXECUTE_LINK_TEXT); // reset default
359+
await setLinkTextForCLILinkSetting(undefined);
360+
const linkTextFromSettings = getLinkTextForCLILinkSetting();
361+
await setLinkTextForCLILinkSetting(DEFAULT_EXECUTE_LINK_TEXT); // reset default
362+
expect(linkTextFromSettings).to.equal(DEFAULT_EXECUTE_LINK_TEXT);
363+
364+
await setLinkTextForCLILinkSetting(''); // blank
365+
const linkTextFromSettings2 = getLinkTextForCLILinkSetting();
366+
await setLinkTextForCLILinkSetting(DEFAULT_EXECUTE_LINK_TEXT); // reset default
367+
expect(linkTextFromSettings2).to.equal(DEFAULT_EXECUTE_LINK_TEXT);
368+
});
369+
320370
});
321371

322372
function checkCanParseDidactUriForPath(urlValue: string, endToCheck: string, alternateEnd : string) {

src/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ export const DIDACT_NOTIFICATION_SETTING = 'didact.disableNotifications';
2828
export const DIDACT_COLUMN_SETTING = 'didact.lastColumnUsed';
2929
export const DIDACT_OPEN_AT_STARTUP = 'didact.openDefaultTutorialAtStartup';
3030
export const DIDACT_AUTO_INSTALL_DEFAULT_TUTORIALS = 'didact.autoAddDefaultTutorials';
31+
export const DIDACT_CLI_LINK_LF_SETTING = 'didact.edit.cliLinkLF';
32+
export const DIDACT_CLI_LINK_TEXT_SETTING = 'didact.edit.cliLinkText';
3133

3234
const CACHED_OUTPUT_CHANNELS: OutputChannel[] = new Array<OutputChannel>();
3335

36+
export const DEFAULT_EXECUTE_LINK_TEXT = '^ execute';
37+
3438
export interface ITutorial {
3539
name: string;
3640
category: string;
@@ -463,3 +467,25 @@ export async function removeTutorialByNameAndCategory(node : TutorialNode ) : Pr
463467
}
464468
return success;
465469
}
470+
471+
export function getInsertLFForCLILinkSetting() : boolean {
472+
return workspace.getConfiguration().get(DIDACT_CLI_LINK_LF_SETTING, true);
473+
}
474+
475+
export function getLinkTextForCLILinkSetting() : string {
476+
const value = workspace.getConfiguration().get(DIDACT_CLI_LINK_TEXT_SETTING, DEFAULT_EXECUTE_LINK_TEXT);
477+
if (value && value.trim().length > 0) {
478+
return value;
479+
}
480+
return DEFAULT_EXECUTE_LINK_TEXT;
481+
}
482+
483+
// for testing
484+
export async function setInsertLFForCLILinkSetting(flag: boolean): Promise<void> {
485+
await workspace.getConfiguration().update(DIDACT_CLI_LINK_LF_SETTING, flag);
486+
}
487+
488+
// for testing
489+
export async function setLinkTextForCLILinkSetting(text: string | undefined): Promise<void> {
490+
await workspace.getConfiguration().update(DIDACT_CLI_LINK_TEXT_SETTING, text);
491+
}

0 commit comments

Comments
 (0)