Skip to content

Commit

Permalink
General usability improvements
Browse files Browse the repository at this point in the history
This commit solves the followings:
- Jump while typing solved by bigger codelens range
- Unfriendly 'no references' messages are gone,
  now the reference name is added if available
- Multiline references got better support. e.g. TS decorators

This resolves #6, #5
  • Loading branch information
kisstkondoros committed Sep 19, 2016
1 parent c4b8577 commit 8985b82
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"type": "string"
},
"typelens.noreferences": {
"default": "no references found",
"default": "no references found for {0}",
"description": "Localization for the case when there are no references found",
"type": "string"
},
Expand Down
45 changes: 31 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from 'path';
import {CodeLensProvider, SymbolInformation, SymbolKind, TextDocument, CancellationToken, CodeLens, Range, Command, Location, commands} from 'vscode';

export function activate(context: vscode.ExtensionContext) {
const standardSymbolKindSet = [SymbolKind.Method, SymbolKind.Function, SymbolKind.Property, SymbolKind.Class, SymbolKind.Interface, SymbolKind.Module];
const standardSymbolKindSet = [SymbolKind.Method, SymbolKind.Function, SymbolKind.Property, SymbolKind.Class, SymbolKind.Interface];
const cssSymbolKindSet = [SymbolKind.Method, SymbolKind.Function, SymbolKind.Property, SymbolKind.Variable];

const SymbolKindInterst = {
Expand All @@ -19,7 +19,7 @@ export function activate(context: vscode.ExtensionContext) {
public exludeself: boolean = true;
public singular: string = "{0} reference";
public plural: string = "{0} references";
public noreferences: string = "no references found";
public noreferences: string = "no references found for {0}";
public unusedcolor: string = "#999";
public decorateunused: boolean = true;
}
Expand Down Expand Up @@ -75,10 +75,10 @@ export function activate(context: vscode.ExtensionContext) {
provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
var settings = this.config.settings;
if (this.unusedDecoration.length > 0 && this.decoration) {
this.unusedDecoration = [];
vscode.window.activeTextEditor.setDecorations(this.decoration, this.unusedDecoration);
this.decoration.dispose();
this.decoration = null;
this.unusedDecoration = [];
}
if (settings.decorateunused) {
this.decoration = vscode.window.createTextEditorDecorationType({
Expand All @@ -94,14 +94,25 @@ export function activate(context: vscode.ExtensionContext) {
}
return knownInterest.indexOf(symbolInformation.kind) > -1;
}).map(symbolInformation => {
var range = symbolInformation.location.range;
var line = document.lineAt(range.start.line);
var index = line.text.lastIndexOf(symbolInformation.name);
var index;
var lineIndex = symbolInformation.location.range.start.line;
do {
var range = symbolInformation.location.range;
var line = document.lineAt(lineIndex);
index = line.text.lastIndexOf(symbolInformation.name);
if (index > -1) {
break;
}
lineIndex++;
} while (lineIndex < symbolInformation.location.range.end.line)

if (index == -1) {
index = line.firstNonWhitespaceCharacterIndex;
range = new Range(range.start, range.start);
} else {
range = new Range(range.start.line, index, range.start.line, index + symbolInformation.name.length);
lineIndex = range.start.line;
range = new Range(lineIndex, index, lineIndex, 90000);
}
else {
range = new Range(lineIndex, index, lineIndex, index + symbolInformation.name.length);
}

return new MethodReferenceLens(new vscode.Range(range.start, range.end), document.uri);
Expand All @@ -116,31 +127,37 @@ export function activate(context: vscode.ExtensionContext) {
if (settings.exludeself) {
filteredLocations = locations.filter(location => !location.range.isEqual(codeLens.range));
}

var isSameDocument = (codeLens.uri == vscode.window.activeTextEditor.document.uri);
var message;
var amount = filteredLocations.length;
if (amount == 0) {
message = settings.noreferences;
var name = isSameDocument ? vscode.window.activeTextEditor.document.getText(codeLens.range) : "";
message = message.replace('{0}', name + "");
} else if (amount == 1) {
message = settings.singular;
message = message.replace('{0}', amount + "");
} else {
message = settings.plural;
message = message.replace('{0}', amount + "");
}
message = message.replace('{0}', amount + "");

if (amount == 0 && settings.decorateunused && this.decoration) {
this.unusedDecoration.push(codeLens.range);
}
vscode.window.activeTextEditor.setDecorations(this.decoration, this.unusedDecoration);

if (isSameDocument) {
vscode.window.activeTextEditor.setDecorations(this.decoration, this.unusedDecoration);
}

if (amount > 0) {
return new CodeLens(codeLens.range, {
return new CodeLens(new vscode.Range(codeLens.range.start.line, codeLens.range.start.character, codeLens.range.start.line, 90000), {
command: 'editor.action.showReferences',
title: message,
arguments: [codeLens.uri, codeLens.range.start, filteredLocations],
});
} else {
return new CodeLens(codeLens.range, {
return new CodeLens(new vscode.Range(codeLens.range.start.line, codeLens.range.start.character, codeLens.range.start.line, 90000), {
command: "editor.action.findReferences",
title: message,
arguments: [codeLens.uri, codeLens.range.start]
Expand Down

0 comments on commit 8985b82

Please sign in to comment.