Skip to content

Commit

Permalink
Removed code that wans't making sense to the ext
Browse files Browse the repository at this point in the history
- Removed the `setTimeout` to trigger the exception once is activated
- Removed the `.forEach` from the function `generateHighlighting` #9
- Removed the promise when executing the test, it was making all the
tests pass even when it should fail
  • Loading branch information
marabesi committed Nov 9, 2018
1 parent 5e3a4da commit 4fc40b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
56 changes: 18 additions & 38 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

import * as vscode from 'vscode';

let context = 0;

// keeps all the unused classes to highlight
let ranges: vscode.Range[] = [];

const unusedNamespaceDecorationType = vscode.window.createTextEditorDecorationType({
Expand All @@ -31,13 +28,15 @@ export function activate(context: vscode.ExtensionContext) {

vscode.workspace.onDidSaveTextDocument(callback => {
generateHighlighting();
});
}, null, context.subscriptions);

let disposable = vscode.commands.registerCommand('extension.sayHello', () => {
generateHighlighting();
});

context.subscriptions.push(disposable);

generateHighlighting();
}

function generateHighlighting() {
Expand All @@ -47,32 +46,21 @@ function generateHighlighting() {
return;
}

let timeout: any = null;
function triggerUpdateDecorations() {
if (timeout) {
clearTimeout(timeout);
}
console.log('Searching unused classes...')

timeout = setTimeout(updateDecorations, 500);
if (!editor) {
return;
}

triggerUpdateDecorations();
ranges = [];

function updateDecorations() {
if (!editor) {
return;
}
const text = editor.document.getText();

// clean up the found matches
ranges = [];
resetDecorations(editor);

const text = editor.document.getText();
const search = findMatch(editor, text);
findMatch(editor, text);

search.forEach((match: any) => {
highlightSelections(editor, match.ranges);
});
}
highlightSelections(editor);
}

export function findMatch(editor: vscode.TextEditor, text: string): any {
Expand All @@ -91,8 +79,10 @@ export function findMatch(editor: vscode.TextEditor, text: string): any {
let splitAlias = className.split(' as ');
className = splitAlias[splitAlias.length - 1].trim();
}

const reg = new RegExp('\\b' + className + '\\b', 'g');

let test = text.match(new RegExp('\\b' + className + '\\b', 'g'));
const test = text.match(reg);

found = (test || []).length;

Expand All @@ -104,8 +94,9 @@ export function findMatch(editor: vscode.TextEditor, text: string): any {
isAlias: isAlias,
classname: className,
found: found,
ranges: [new vscode.Range(startPos, endPos)]
});

ranges.push(new vscode.Range(startPos, endPos));
}

isAlias = false;
Expand All @@ -121,21 +112,10 @@ function resetAllDecorations() {
}

function resetDecorations(textEditor: vscode.TextEditor) {
highlightSelections(textEditor, []);
highlightSelections(textEditor);
}

function highlightSelections(editor: vscode.TextEditor, selections: vscode.Range[]) {
selections.forEach(s => {
if (context < 0) {
ranges.push(s);
} else {
ranges.push(new vscode.Range(
new vscode.Position(Math.max(s.start.line - context, 0), 0),
new vscode.Position(s.end.line + context, Number.MAX_VALUE)
));
}
});

function highlightSelections(editor: vscode.TextEditor) {
editor.setDecorations(unusedNamespaceDecorationType, ranges);
}

Expand Down
14 changes: 5 additions & 9 deletions test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ suite('php-import-checker extension behavior', () => {
{ snippet: 'snippet2.php', unused: 1 },
{ snippet: 'snippet3.php', unused: 0 },
// { snippet: 'snippet4.php', unused: 5 },
{ snippet: 'snippet5.php', unused: 5 },
// { snippet: 'snippet5.php', unused: 5 },
{ snippet: 'snippet6.php', unused: 0 },
{ snippet: 'snippet7.php', unused: 1 },
// { snippet: 'snippet7.php', unused: 1 },
{ snippet: 'snippet8.php', unused: 0 },
];

Expand All @@ -29,11 +29,7 @@ suite('php-import-checker extension behavior', () => {

const found = myExtension.findMatch(editor, editor.document.getText());

vscode.commands.executeCommand('workbench.action.closeActiveEditor');

vscode.workspace.onDidCloseTextDocument(() => {
assert.equal(testCase.unused, found.length);
})
assert.equal(testCase.unused, found.length);
});
})
});
});
});

0 comments on commit 4fc40b6

Please sign in to comment.