Skip to content

Commit

Permalink
Context menu support (clarkio#115)
Browse files Browse the repository at this point in the history
* Remove highlights by context-menu

* Updated changelog
  • Loading branch information
parithon committed Mar 8, 2020
1 parent 443093a commit 06065a6
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Released]

## [0.5.1]

### Added

- Context menu support to remove highlights. You can now remove highlights by right-clicking on them. Additionally, you can remove all highlights by right-clicking the editor tab.

## [0.5.0]

### Rewrite
Expand Down
25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "twitch-highlighter",
"displayName": "Twitch Highlighter",
"description": "Allow your Twitch viewers to help in spotting bugs, typos, etc. by sending a command in chat that will highlight the line of code they want you to check.",
"version": "0.5.0",
"version": "0.6.0",
"preview": true,
"publisher": "clarkio",
"engines": {
Expand Down Expand Up @@ -84,6 +84,11 @@
"command": "twitchHighlighter.disconnect",
"title": "Stop Listening to Chat",
"category": "Twitch Highlighter"
},
{
"command": "twitchHighlighter.context.unhighlight",
"title": "Remove Highlight",
"category": "Twitch Highlighter"
}
],
"menus": {
Expand Down Expand Up @@ -111,6 +116,10 @@
{
"command": "twitchHighlighter.requestUnhighlightAll",
"when": "false"
},
{
"command": "twitchHighlighter.context.unhighlight",
"when": "false"
}
],
"view/title": [
Expand All @@ -126,6 +135,20 @@
"when": "view == twitchHighlighterTreeView || view == twitchHighlighterTreeView-explorer || view == twitchHighlighterTreeView-debug",
"group": "edit"
}
],
"editor/context": [
{
"command": "twitchHighlighter.context.unhighlight",
"group": "1_modification",
"when": "editorHasHighlights"
}
],
"editor/title/context": [
{
"command": "twitchHighlighter.unhighlightAll",
"group": "3_open",
"when": "editorHasHighlights"
}
]
},
"viewsContainers": {
Expand Down
32 changes: 30 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';

import { HighlighterAPI, IHighlightRequested, IUnhighlightRequested, IUnhighlightAllRequested } from './api';
import { Commands, LogLevel, Configuration, Settings } from './enums';
import { Commands, LogLevel, Configuration, Settings, AppContexts } from './enums';
import { Logger, log } from './logger';
import {
HighlightManager,
Expand Down Expand Up @@ -56,7 +56,9 @@ export class App implements vscode.Disposable {

vscode.commands.registerCommand(Commands.requestHighlight, this.requestHighlightHandler, this),
vscode.commands.registerCommand(Commands.requestUnhighlight, this.requestUnhighlightHandler, this),
vscode.commands.registerCommand(Commands.requestUnhighlightAll, this.requestUnhighlightAllHandler, this)
vscode.commands.registerCommand(Commands.requestUnhighlightAll, this.requestUnhighlightAllHandler, this),

vscode.commands.registerCommand(Commands.contextMenuUnhighlight, this.contextMenuUnhighlightHandler, this)
);

this.log('Initialized line highlighter.');
Expand Down Expand Up @@ -126,13 +128,31 @@ export class App implements vscode.Disposable {
}
}

/**
* Sets the 'editorHasHighlights' to true or false.
* The 'editorHasHighlights' context is used to determine if the
* 'Remove Highlight' and 'Remove All Highlights' context menu items
* are visible or not.
*/
private setEditorHasHighlightsContext() {
if (vscode.window.activeTextEditor) {
const editor = vscode.window.activeTextEditor;
if (this._highlightManager.GetDecorations(editor.document.fileName).length > 0) {
vscode.commands.executeCommand('setContext', AppContexts.editorHasHighlights, true);
} else {
vscode.commands.executeCommand('setContext', AppContexts.editorHasHighlights, false);
}
}
}

private onDidChangeActiveTextEditorHandler(editor?: vscode.TextEditor): void {
if (editor) {
this.currentDocument = editor.document;
}
else {
this.currentDocument = undefined;
}
this.setEditorHasHighlightsContext();
}

private refreshTreeviewHandler(): void {
Expand All @@ -154,6 +174,7 @@ export class App implements vscode.Disposable {
}

private refresh(): void {
this.setEditorHasHighlightsContext();
vscode.window.visibleTextEditors.forEach(te => {
te.setDecorations(
this.highlightDecorationType,
Expand Down Expand Up @@ -293,4 +314,11 @@ export class App implements vscode.Disposable {
this._highlightManager.Clear(service);
this._onUnhighlightAllRequested.fire({service, callerId});
}

private contextMenuUnhighlightHandler() {
if (vscode.window.activeTextEditor) {
const lineNumber = vscode.window.activeTextEditor.selection.active.line;
this._highlightManager.Remove(vscode.window.activeTextEditor.document, 'self', lineNumber + 1);
}
}
}
3 changes: 3 additions & 0 deletions src/enums/AppContexts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum AppContexts {
'editorHasHighlights' = 'editorHasHighlights'
}
3 changes: 2 additions & 1 deletion src/enums/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export enum Commands {
'signIn' = 'twitchHighlighter.signIn',
'signOut' = 'twitchHighlighter.signOut',
'connect' = 'twitchHighlighter.connect',
'disconnect' = 'twitchHighlighter.disconnect'
'disconnect' = 'twitchHighlighter.disconnect',
'contextMenuUnhighlight' = 'twitchHighlighter.context.unhighlight'
}
1 change: 1 addition & 0 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './LogLevel';
export * from './Configuration';
export * from './KeytarKeys';
export * from './TwitchKeys';
export * from './AppContexts';
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ export function deactivate() {
ttvchat.dispose();
liveshare.dispose();
}

export const editorHasDecorations = () => {
return true;
}
8 changes: 8 additions & 0 deletions types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export declare class App implements vscode.Disposable {
private onDidChangeTextDocumentHandler;
private onDidChangeConfigurationHandler;
private onDidChangeVisibleTextEditorsHandler;
/**
* Sets the 'editorHasHighlights' to true or false.
* The 'editorHasHighlights' context is used to determine if the
* 'Remove Highlight' and 'Remove All Highlights' context menu items
* are visible or not.
*/
private setEditorHasHighlightsContext;
private onDidChangeActiveTextEditorHandler;
private refreshTreeviewHandler;
private createTextEditorDecorationType;
Expand All @@ -31,4 +38,5 @@ export declare class App implements vscode.Disposable {
private requestHighlightHandler;
private requestUnhighlightHandler;
private requestUnhighlightAllHandler;
private contextMenuUnhighlightHandler;
}
3 changes: 2 additions & 1 deletion types/enums/Commands.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export declare enum Commands {
'signIn' = "twitchHighlighter.signIn",
'signOut' = "twitchHighlighter.signOut",
'connect' = "twitchHighlighter.connect",
'disconnect' = "twitchHighlighter.disconnect"
'disconnect' = "twitchHighlighter.disconnect",
'contextMenuUnhighlight' = "twitchHighlighter.context.unhighlight"
}
1 change: 1 addition & 0 deletions types/enums/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './LogLevel';
export * from './Configuration';
export * from './KeytarKeys';
export * from './TwitchKeys';
export * from './AppContexts';
1 change: 1 addition & 0 deletions types/extension.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as vscode from 'vscode';
export declare function activate(context: vscode.ExtensionContext): import("./api").HighlighterAPI;
export declare function deactivate(): void;
export declare const editorHasDecorations: () => boolean;

0 comments on commit 06065a6

Please sign in to comment.