Fix remaining tslint suggestions

This commit is contained in:
Adolfo Ochagavía 2018-10-08 20:18:55 +02:00
parent 4d62cfccbb
commit 62b1b05a0d
15 changed files with 82 additions and 78 deletions

View file

@ -1,17 +0,0 @@
import * as applySourceChange from './commands/apply_source_change';
import * as extendSelection from './commands/extend_selection';
import * as joinLines from './commands/join_lines';
import * as matchingBrace from './commands/matching_brace';
import * as parentModule from './commands/parent_module';
import * as runnables from './commands/runnables';
import * as syntaxTree from './commands/syntaxTree';
export {
applySourceChange,
extendSelection,
joinLines,
matchingBrace,
parentModule,
runnables,
syntaxTree,
};

View file

@ -18,7 +18,6 @@ export interface SourceChange {
} }
export async function handle(change: SourceChange) { export async function handle(change: SourceChange) {
console.log(`applySOurceChange ${JSON.stringify(change)}`);
const wsEdit = new vscode.WorkspaceEdit(); const wsEdit = new vscode.WorkspaceEdit();
for (const sourceEdit of change.sourceFileEdits) { for (const sourceEdit of change.sourceFileEdits) {
const uri = Server.client.protocol2CodeConverter.asUri(sourceEdit.textDocument.uri); const uri = Server.client.protocol2CodeConverter.asUri(sourceEdit.textDocument.uri);
@ -28,17 +27,18 @@ export async function handle(change: SourceChange) {
let created; let created;
let moved; let moved;
for (const fsEdit of change.fileSystemEdits) { for (const fsEdit of change.fileSystemEdits) {
if (fsEdit.type == 'createFile') { switch (fsEdit.type) {
case 'createFile':
const uri = vscode.Uri.parse(fsEdit.uri!); const uri = vscode.Uri.parse(fsEdit.uri!);
wsEdit.createFile(uri); wsEdit.createFile(uri);
created = uri; created = uri;
} else if (fsEdit.type == 'moveFile') { break;
case 'moveFile':
const src = vscode.Uri.parse(fsEdit.src!); const src = vscode.Uri.parse(fsEdit.src!);
const dst = vscode.Uri.parse(fsEdit.dst!); const dst = vscode.Uri.parse(fsEdit.dst!);
wsEdit.renameFile(src, dst); wsEdit.renameFile(src, dst);
moved = dst; moved = dst;
} else { break;
console.error(`unknown op: ${JSON.stringify(fsEdit)}`);
} }
} }
const toOpen = created || moved; const toOpen = created || moved;
@ -51,7 +51,7 @@ export async function handle(change: SourceChange) {
const uri = Server.client.protocol2CodeConverter.asUri(toReveal.textDocument.uri); const uri = Server.client.protocol2CodeConverter.asUri(toReveal.textDocument.uri);
const position = Server.client.protocol2CodeConverter.asPosition(toReveal.position); const position = Server.client.protocol2CodeConverter.asPosition(toReveal.position);
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (!editor || editor.document.uri.toString() != uri.toString()) { return; } if (!editor || editor.document.uri.toString() !== uri.toString()) { return; }
if (!editor.selection.isEmpty) { return; } if (!editor.selection.isEmpty) { return; }
editor!.selection = new vscode.Selection(position, position); editor!.selection = new vscode.Selection(position, position);
} }

View file

@ -14,7 +14,7 @@ interface ExtendSelectionResult {
export async function handle() { export async function handle() {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId != 'rust') { return; } if (editor == null || editor.document.languageId !== 'rust') { return; }
const request: ExtendSelectionParams = { const request: ExtendSelectionParams = {
selections: editor.selections.map((s) => { selections: editor.selections.map((s) => {
return Server.client.code2ProtocolConverter.asRange(s); return Server.client.code2ProtocolConverter.asRange(s);

View file

@ -0,0 +1,17 @@
import * as applySourceChange from './apply_source_change';
import * as extendSelection from './extend_selection';
import * as joinLines from './join_lines';
import * as matchingBrace from './matching_brace';
import * as parentModule from './parent_module';
import * as runnables from './runnables';
import * as syntaxTree from './syntaxTree';
export {
applySourceChange,
extendSelection,
joinLines,
matchingBrace,
parentModule,
runnables,
syntaxTree,
};

View file

@ -11,7 +11,7 @@ interface JoinLinesParams {
export async function handle() { export async function handle() {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId != 'rust') { return; } if (editor == null || editor.document.languageId !== 'rust') { return; }
const request: JoinLinesParams = { const request: JoinLinesParams = {
range: Server.client.code2ProtocolConverter.asRange(editor.selection), range: Server.client.code2ProtocolConverter.asRange(editor.selection),
textDocument: { uri: editor.document.uri.toString() }, textDocument: { uri: editor.document.uri.toString() },

View file

@ -10,7 +10,7 @@ interface FindMatchingBraceParams {
export async function handle() { export async function handle() {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId != 'rust') { return; } if (editor == null || editor.document.languageId !== 'rust') { return; }
const request: FindMatchingBraceParams = { const request: FindMatchingBraceParams = {
textDocument: { uri: editor.document.uri.toString() }, textDocument: { uri: editor.document.uri.toString() },
offsets: editor.selections.map((s) => { offsets: editor.selections.map((s) => {

View file

@ -5,7 +5,7 @@ import { Server } from '../server';
export async function handle() { export async function handle() {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId != 'rust') { return; } if (editor == null || editor.document.languageId !== 'rust') { return; }
const request: TextDocumentIdentifier = { const request: TextDocumentIdentifier = {
uri: editor.document.uri.toString(), uri: editor.document.uri.toString(),
}; };

View file

@ -59,7 +59,7 @@ function createTask(spec: Runnable): vscode.Task {
let prevRunnable: RunnableQuickPick | undefined; let prevRunnable: RunnableQuickPick | undefined;
export async function handle() { export async function handle() {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
if (editor == null || editor.document.languageId != 'rust') { return; } if (editor == null || editor.document.languageId !== 'rust') { return; }
const textDocument: lc.TextDocumentIdentifier = { const textDocument: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString(), uri: editor.document.uri.toString(),
}; };
@ -73,7 +73,7 @@ export async function handle() {
items.push(prevRunnable); items.push(prevRunnable);
} }
for (const r of runnables) { for (const r of runnables) {
if (prevRunnable && JSON.stringify(prevRunnable.runnable) == JSON.stringify(r)) { if (prevRunnable && JSON.stringify(prevRunnable.runnable) === JSON.stringify(r)) {
continue; continue;
} }
items.push(new RunnableQuickPick(r)); items.push(new RunnableQuickPick(r));

View file

@ -1,7 +0,0 @@
import * as changeActiveTextEditor from './events/change_active_text_editor';
import * as changeTextDocument from './events/change_text_document';
export {
changeActiveTextEditor,
changeTextDocument,
};

View file

@ -5,7 +5,7 @@ import { Decoration } from '../highlighting';
import { Server } from '../server'; import { Server } from '../server';
export async function handle(editor: TextEditor | undefined) { export async function handle(editor: TextEditor | undefined) {
if (!Server.config.highlightingOn || !editor || editor.document.languageId != 'rust') { return; } if (!Server.config.highlightingOn || !editor || editor.document.languageId !== 'rust') { return; }
const params: TextDocumentIdentifier = { const params: TextDocumentIdentifier = {
uri: editor.document.uri.toString(), uri: editor.document.uri.toString(),
}; };

View file

@ -5,7 +5,7 @@ import { syntaxTreeUri, TextDocumentContentProvider } from '../commands/syntaxTr
export function createHandler(textDocumentContentProvider: TextDocumentContentProvider) { export function createHandler(textDocumentContentProvider: TextDocumentContentProvider) {
return (event: vscode.TextDocumentChangeEvent) => { return (event: vscode.TextDocumentChangeEvent) => {
const doc = event.document; const doc = event.document;
if (doc.languageId != 'rust') { return; } if (doc.languageId !== 'rust') { return; }
afterLs(() => { afterLs(() => {
textDocumentContentProvider.eventEmitter.fire(syntaxTreeUri); textDocumentContentProvider.eventEmitter.fire(syntaxTreeUri);
}); });

View file

@ -0,0 +1,7 @@
import * as changeActiveTextEditor from './change_active_text_editor';
import * as changeTextDocument from './change_text_document';
export {
changeActiveTextEditor,
changeTextDocument,
};

View file

@ -9,17 +9,42 @@ export interface Decoration {
} }
export class Highlighter { export class Highlighter {
private decorations: { [index: string]: vscode.TextEditorDecorationType }; private static initDecorations(): Map<string, vscode.TextEditorDecorationType> {
constructor() { const decor = (color: string) => vscode.window.createTextEditorDecorationType({ color });
this.decorations = {};
const decorations: Iterable<[string, vscode.TextEditorDecorationType]> = [
['background', decor('#3F3F3F')],
['error', vscode.window.createTextEditorDecorationType({
borderColor: 'red',
borderStyle: 'none none dashed none',
})],
['comment', decor('#7F9F7F')],
['string', decor('#CC9393')],
['keyword', decor('#F0DFAF')],
['function', decor('#93E0E3')],
['parameter', decor('#94BFF3')],
['builtin', decor('#DD6718')],
['text', decor('#DCDCCC')],
['attribute', decor('#BFEBBF')],
['literal', decor('#DFAF8F')],
];
return new Map<string, vscode.TextEditorDecorationType>(decorations);
} }
private decorations: (Map<string, vscode.TextEditorDecorationType> | null) = null;
public removeHighlights() { public removeHighlights() {
for (const tag in this.decorations) { if (this.decorations == null) {
this.decorations[tag].dispose(); return;
} }
this.decorations = {}; // Decorations are removed when the object is disposed
for (const decoration of this.decorations.values()) {
decoration.dispose();
}
this.decorations = null;
} }
public setHighlights( public setHighlights(
@ -30,18 +55,17 @@ export class Highlighter {
// //
// Note: decoration objects need to be kept around so we can dispose them // Note: decoration objects need to be kept around so we can dispose them
// if the user disables syntax highlighting // if the user disables syntax highlighting
if (Object.keys(this.decorations).length === 0) { if (this.decorations == null) {
this.initDecorations(); this.decorations = Highlighter.initDecorations();
} }
const byTag: Map<string, vscode.Range[]> = new Map(); const byTag: Map<string, vscode.Range[]> = new Map();
for (const tag in this.decorations) { for (const tag of this.decorations.keys()) {
byTag.set(tag, []); byTag.set(tag, []);
} }
for (const d of highlights) { for (const d of highlights) {
if (!byTag.get(d.tag)) { if (!byTag.get(d.tag)) {
console.log(`unknown tag ${d.tag}`);
continue; continue;
} }
byTag.get(d.tag)!.push( byTag.get(d.tag)!.push(
@ -50,29 +74,9 @@ export class Highlighter {
} }
for (const tag of byTag.keys()) { for (const tag of byTag.keys()) {
const dec: vscode.TextEditorDecorationType = this.decorations[tag]; const dec = this.decorations.get(tag) as vscode.TextEditorDecorationType;
const ranges = byTag.get(tag)!; const ranges = byTag.get(tag)!;
editor.setDecorations(dec, ranges); editor.setDecorations(dec, ranges);
} }
} }
private initDecorations() {
const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj });
this.decorations = {
background: decor('#3F3F3F'),
error: vscode.window.createTextEditorDecorationType({
borderColor: 'red',
borderStyle: 'none none dashed none',
}),
comment: decor('#7F9F7F'),
string: decor('#CC9393'),
keyword: decor('#F0DFAF'),
function: decor('#93E0E3'),
parameter: decor('#94BFF3'),
builtin: decor('#DD6718'),
text: decor('#DCDCCC'),
attribute: decor('#BFEBBF'),
literal: decor('#DFAF8F'),
};
}
} }

View file

@ -34,7 +34,7 @@ export class Server {
'm/publishDecorations', 'm/publishDecorations',
(params: PublishDecorationsParams) => { (params: PublishDecorationsParams) => {
const targetEditor = vscode.window.visibleTextEditors.find( const targetEditor = vscode.window.visibleTextEditors.find(
(editor) => editor.document.uri.toString() == params.uri, (editor) => editor.document.uri.toString() === params.uri,
); );
if (!Server.config.highlightingOn || !targetEditor) { return; } if (!Server.config.highlightingOn || !targetEditor) { return; }
Server.highlighter.setHighlights( Server.highlighter.setHighlights(

View file

@ -1,5 +1,5 @@
{ {
"defaultSeverity": "warning", "defaultSeverity": "error",
"extends": [ "extends": [
"tslint:recommended" "tslint:recommended"
], ],