Skip to content

Commit

Permalink
fix: Issues relating to the RegExp Tester (#558)
Browse files Browse the repository at this point in the history
* Add overrides to improve spell checker speed

* fix: do not show Test command if view not enabled.

* Update cspell-words.txt

* fix: be able to match against regexp as well as name.

* fix: trim long names.

* fix: only perform actions when regex view is turned on.
  • Loading branch information
Jason3S authored Sep 27, 2020
1 parent d782f72 commit 176c016
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 43 deletions.
1 change: 1 addition & 0 deletions cspell-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ devtools
dict
disqus
downlevel
enablement
endhighlight
envs
gemfiles
Expand Down
2 changes: 1 addition & 1 deletion packages/_server/src/PatternMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function resolvePattern(pat: string | NamedPattern, knownPatterns: Map<string, P
if (isNamedPattern(pat)) {
return {...pat, regexp: toRegExp(pat.regexp)};
}
return knownPatterns.get(pat.toLowerCase()) || ({ name: pat, regexp: toRegExp(pat, 'g')});
return knownPatterns.get(pat) || knownPatterns.get(pat.toLowerCase()) || ({ name: pat, regexp: toRegExp(pat, 'g')});
}

function isNamedPattern(pattern: string | NamedPattern): pattern is NamedPattern {
Expand Down
26 changes: 26 additions & 0 deletions packages/_server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,35 @@ const tds = CSpell;

const defaultCheckLimit = Validator.defaultCheckLimit;

export const regExSpellingGuardBlock = /(\bc?spell(?:-?checker)?::?)\s*disable(?!-line|-next)\b[\s\S]*?((?:\1\s*enable\b)|$)/gi;
export const regExSpellingGuardNext = /\bc?spell(?:-?checker)?::?\s*disable-next\b.*\s.*/gi;
export const regExSpellingGuardLine = /^.*\bc?spell(?:-?checker)?::?\s*disable-line\b.*/gim;


const overRideDefaults: CSpellUserSettings = {
id: 'Extension overrides',
patterns: [
{
// Turn off the build-in expression by matching the beginning of the doc.
name: 'SpellCheckerDisable',
pattern: /^(?=.?)/,
},
{
name: 'SpellCheckerDisableBlock',
pattern: regExSpellingGuardBlock,
},
{
name: 'SpellCheckerDisableNext',
pattern: regExSpellingGuardNext,
},
{
name: 'SpellCheckerDisableLine',
pattern: regExSpellingGuardLine,
},
],
ignoreRegExpList: [
'SpellCheckerDisableBlock', 'SpellCheckerDisableNext', 'SpellCheckerDisableLine',
]
};

// Turn off the spell checker by default. The setting files should have it set.
Expand Down
9 changes: 8 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@
"when": "editorTextFocus && config.cSpell.showCommandsInEditorContextMenu",
"group": "A_cspell@9"
}
]
],
"commandPalette": [
{
"command": "cSpellRegExpTester.testRegExp",
"title": "Test a Regular Expression on the current document X.",
"when": "config.cSpell.experimental.enableRegexpView"
}
]
},
"views": {
"explorer": [
Expand Down
12 changes: 10 additions & 2 deletions packages/client/src/extensionRegEx/RegexpOutlineProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { MatchPatternsToDocumentResult, PatternMatch } from '../server';
import { PatternMatch } from '../server';

export class RegexpOutlineProvider implements vscode.TreeDataProvider<OutlineItem> {

Expand Down Expand Up @@ -52,7 +52,7 @@ function createCategoryItem(category: string, matches: PatternMatch[]): OutlineI
}

function createLeaf(offset: PatternMatch): OutlineItem {
const treeItem = new vscode.TreeItem(offset.name);
const treeItem = new vscode.TreeItem(trimName(offset.name));
const timeMs = offset.elapsedTime.toFixed(2);
const msg = offset.message ? ' ' + offset.message : ''
const parts = [
Expand All @@ -74,3 +74,11 @@ function createLeaf(offset: PatternMatch): OutlineItem {

return item;
}

function trimName(name: string) {
const maxLen = 50;
if (name.length <= maxLen) {
return name;
}
return name.substr(0, maxLen - 1) + '…';
}
71 changes: 32 additions & 39 deletions packages/client/src/extensionRegEx/extensionRegEx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ interface DisposableLike {
dispose(): any;
}

interface InProgress {
activeEditor: vscode.TextEditor;
document: vscode.TextDocument;
version: number;
}

// this method is called when vs code is activated
export function activate(context: vscode.ExtensionContext, client: CSpellClient) {
export function activate(context: vscode.ExtensionContext, client: CSpellClient): void {

const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);

Expand Down Expand Up @@ -43,31 +37,23 @@ export function activate(context: vscode.ExtensionContext, client: CSpellClient)
}
});

let activeEditor = vscode.window.activeTextEditor;
let pattern: string | undefined = '/\\w+/';

let isActive = fetchIsEnabledFromConfig();

let activeEditor = isActive ? vscode.window.activeTextEditor : undefined;
let pattern: string | undefined = undefined;

async function updateDecorations() {
disposeCurrent();
if (!isActive) {
activeEditor?.setDecorations(decorationTypeExclude, []);
statusBar.hide();
if (!isActive || !activeEditor) {
clearDecorations();
return;
}

if (!activeEditor) {
return;
}
if (!pattern) {
activeEditor.setDecorations(decorationTypeExclude, []);
statusBar.hide();
return;
}
const userPatterns = pattern ? [pattern] : [];
const document = activeEditor.document;
const version = document.version;
const config = await client.getConfigurationForDocument(document);
const extractedPatterns = extractPatternsFromConfig(config.docSettings, [pattern]);
const extractedPatterns = extractPatternsFromConfig(config.docSettings, userPatterns);
const patterns = extractedPatterns.map(p => p.pattern);

client.matchPatternsInDocument(document, patterns).then(result => {
Expand Down Expand Up @@ -110,6 +96,11 @@ export function activate(context: vscode.ExtensionContext, client: CSpellClient)
});
}

function clearDecorations() {
activeEditor?.setDecorations(decorationTypeExclude, []);
statusBar.hide();
}

function createHoverMessage(match: PatternMatch) {
const r = (new vscode.MarkdownString())
.appendText(match.name + ' ' + match.elapsedTime.toFixed(2) + 'ms');
Expand All @@ -131,7 +122,7 @@ export function activate(context: vscode.ExtensionContext, client: CSpellClient)
}

function updateStatusBar(pattern: string | undefined, info?: StatusBarInfo) {
if (pattern) {
if (isActive && pattern) {
const { elapsedTime, count = 0 } = info || {};
const time = elapsedTime ? `${elapsedTime.toFixed(2)}ms` : '$(clock)';
statusBar.text = `${time} | ${pattern}`;
Expand All @@ -148,14 +139,16 @@ export function activate(context: vscode.ExtensionContext, client: CSpellClient)
}

vscode.window.onDidChangeActiveTextEditor(editor => {
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
if (isActive) {
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
}
}
}, null, context.subscriptions);

vscode.workspace.onDidChangeTextDocument(event => {
if (activeEditor && event.document === activeEditor.document) {
if (isActive && activeEditor && event.document === activeEditor.document) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
Expand All @@ -179,17 +172,7 @@ export function activate(context: vscode.ExtensionContext, client: CSpellClient)
value: pattern?.toString(),
validateInput
}).then(value => {
if (!value) {
pattern = undefined;
triggerUpdateDecorations();
return;
}
try {
pattern = value;
} catch (e) {
vscode.window.showWarningMessage(e.toString());
pattern = undefined;
}
pattern = value ? value : undefined;
triggerUpdateDecorations();
});
}
Expand All @@ -204,7 +187,17 @@ export function activate(context: vscode.ExtensionContext, client: CSpellClient)
}

function updateIsActive() {
const currentIsActive = isActive;
isActive = fetchIsEnabledFromConfig();
if (currentIsActive == isActive) {
return;
}
if (isActive) {
activeEditor = vscode.window.activeTextEditor;
triggerUpdateDecorations();
} else {
clearDecorations();
}
}

function dispose() {
Expand Down Expand Up @@ -244,5 +237,5 @@ function extractPatternsFromConfig(config: CSpellUserSettings | undefined, userP

function fetchIsEnabledFromConfig(): boolean {
const cfg = vscode.workspace.getConfiguration('cSpell');
return !!cfg && !!cfg.get('experimental.enableRegexpView');
return !!cfg?.get('experimental.enableRegexpView');
}

0 comments on commit 176c016

Please sign in to comment.