Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions out/extension.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion out/extension.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@
"language": "duckyscript",
"path": "./snippets/snippets.json"
}
],
"configuration": {
"type": "object",
"title": "DuckyScript",
"properties": {
"duckyscript.customCommands": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"scope": "resource",
"markdownDescription": "Extra DuckyScript commands to highlight, for firmware that extends the language (e.g. [USB Army Knife](https://github.com/i-am-shodan/USBArmyKnife/wiki/Commands), Flipper Zero). Matching is case-insensitive and applies to the first word of a line."
}
}
},
"semanticTokenTypes": [
{
"id": "duckyscriptCustomCommand",
"superType": "function",
"description": "A user-defined DuckyScript command from duckyscript.customCommands."
}
],
"semanticTokenScopes": [
{
"language": "duckyscript",
"scopes": {
"duckyscriptCustomCommand": [
"support.function.builtin.duckyscript"
]
}
}
]
},
"scripts": {
Expand Down
76 changes: 76 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,71 @@ const updatedCommandGroups = commandGroups.map(group =>

const ALL_COMMANDS = commandGroups.flat();

const CUSTOM_COMMAND_TOKEN = 'duckyscriptCustomCommand';

const semanticTokenLegend = new vscode.SemanticTokensLegend([CUSTOM_COMMAND_TOKEN], []);

function escapeRegExp(text: string): string {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

class CustomCommandSemanticTokensProvider implements vscode.DocumentSemanticTokensProvider, vscode.Disposable {
private commandPattern: RegExp | undefined;
private readonly _emitter = new vscode.EventEmitter<void>();
private readonly _configListener: vscode.Disposable;
onDidChangeSemanticTokens = this._emitter.event;

constructor() {
this.updateCustomCommands();
this._configListener = vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('duckyscript.customCommands')) {
this.updateCustomCommands();
this._emitter.fire();
}
});
}

private updateCustomCommands() {
const config = vscode.workspace.getConfiguration('duckyscript');
const commands = config.get<string[]>('customCommands', [])
.filter(c => typeof c === 'string' && c.trim().length > 0)
.map(c => c.trim())
// Longest first, so PREFIX_LONG wins over PREFIX.
.sort((a, b) => b.length - a.length);

this.commandPattern = commands.length
? new RegExp(`^\\s*(${commands.map(escapeRegExp).join('|')})(?![\\w-])`, 'i')
: undefined;
}

provideDocumentSemanticTokens(
document: vscode.TextDocument,
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.SemanticTokens> {
const builder = new vscode.SemanticTokensBuilder(semanticTokenLegend);
if (!this.commandPattern) {
return builder.build();
}

for (let i = 0; i < document.lineCount; i++) {
if (token.isCancellationRequested) {
return builder.build();
}
const match = this.commandPattern.exec(document.lineAt(i).text);
if (match) {
const startColumn = match[0].length - match[1].length;
builder.push(i, startColumn, match[1].length, 0, 0);
}
}
return builder.build();
}

dispose() {
this._configListener.dispose();
this._emitter.dispose();
}
}

// We implement a CompletionItemProvider for our language
class MyLanguageCompletionItemProvider implements vscode.CompletionItemProvider {
// This method is called when the user activates the suggestions (e.g., Ctrl+Space)
Expand Down Expand Up @@ -98,6 +163,17 @@ export function activate(context: vscode.ExtensionContext) {
);

context.subscriptions.push(providerDisposable);

// Custom commands syntax highlighting
const customCommandProvider = new CustomCommandSemanticTokensProvider();
context.subscriptions.push(
customCommandProvider,
vscode.languages.registerDocumentSemanticTokensProvider(
{ language: 'duckyscript' },
customCommandProvider,
semanticTokenLegend
)
);
}

function deactivate() { }
Expand Down
2 changes: 1 addition & 1 deletion themes/DarkDuckyScript-color-theme.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "DuckyScript - Night Owl based",
"type": "dark",
"semanticHighlighting": false,
"semanticHighlighting": true,
"colors": {
"contrastBorder": "#122d42",
"focusBorder": "#122d42",
Expand Down
2 changes: 1 addition & 1 deletion themes/LightDuckyScript-color-theme.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "DuckyScript - Night Owl Light based",
"type": "light",
"semanticHighlighting": false,
"semanticHighlighting": true,
"colors": {
// Base
"foreground": "#403f53",
Expand Down