From 751ec708a0e2820857d0826041b34190649d8690 Mon Sep 17 00:00:00 2001 From: Sam Killgallon Date: Mon, 5 Mar 2018 22:19:19 +0000 Subject: [PATCH] Debounce linting to prevent running on every keypress --- package.json | 5 +++++ readme.md | 6 +++++- src/ruby.ts | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a05a080a5..2c59faef5 100644 --- a/package.json +++ b/package.json @@ -161,6 +161,11 @@ "description": "Path to the rct-complete command. Set this to an absolute path to select from multiple installed Ruby versions.", "isExecutable": true }, + "ruby.lintDebounceTime": { + "type": "integer", + "default": 500, + "description": "Time (ms) to wait after keypress before running enabled linters. Ensures linters are only run when typing has finished and not for every keypress" + }, "ruby.lint": { "type": "object", "description": "Set individual ruby linters to use", diff --git a/readme.md b/readme.md index 1cae43032..514ea0049 100644 --- a/readme.md +++ b/readme.md @@ -58,7 +58,11 @@ Enable each one in your workspace or user settings: "fasterer": true, "debride": true, "ruby-lint": true -} +}, + +// Time (ms) to wait after keypress before running enabled linters. Ensures +// linters are only run when typing has finished and not for every keypress +"ruby.lintDebounceTime": 500, //advanced: set command line options for some linters: "ruby.lint": { diff --git a/src/ruby.ts b/src/ruby.ts index d1e5247c4..00db5da4b 100644 --- a/src/ruby.ts +++ b/src/ruby.ts @@ -10,6 +10,7 @@ import { RubyDocumentFormattingEditProvider } from './format/rubyFormat'; import * as utils from './utils'; import { registerTaskProvider } from './task/rake'; import { Config as LintConfig } from './lint/lintConfig'; +import * as debounce from 'lodash/debounce'; export function activate(context: ExtensionContext) { const subs = context.subscriptions; @@ -131,8 +132,12 @@ function registerLinters(ctx: ExtensionContext) { linters.run(e.document); } + // Debounce linting to prevent running on every keypress, only run when typing has stopped + const lintDebounceTime = vscode.workspace.getConfiguration('ruby').lintDebounceTime; + const executeDebouncedLinting = debounce(executeLinting, lintDebounceTime); + ctx.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(executeLinting)); - ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument(executeLinting)); + ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument(executeDebouncedLinting)); ctx.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => { const docs = vscode.window.visibleTextEditors.map(editor => editor.document); console.log("Config changed. Should lint:", docs.length);