Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 075bfc7

Browse files
committed
Move linters provider to standalone file
1 parent 1a0050a commit 075bfc7

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

src/providers/linters.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as vscode from 'vscode';
2+
import { ExtensionContext } from 'vscode';
3+
import * as debounce from 'lodash/debounce';
4+
5+
import { LintCollection } from '../lint/lintCollection';
6+
import { Config as LintConfig } from '../lint/lintConfig';
7+
8+
function getGlobalLintConfig() : LintConfig {
9+
let globalConfig = new LintConfig();
10+
11+
let pathToRuby = vscode.workspace.getConfiguration("ruby.interpreter").commandPath;
12+
if (pathToRuby) {
13+
globalConfig.pathToRuby = pathToRuby;
14+
}
15+
16+
let useBundler = vscode.workspace.getConfiguration("ruby").get<boolean | null>("useBundler");
17+
if (useBundler !== null) {
18+
globalConfig.useBundler = useBundler;
19+
}
20+
21+
let pathToBundler = vscode.workspace.getConfiguration("ruby").pathToBundler;
22+
if (pathToBundler) {
23+
globalConfig.pathToBundler = pathToBundler;
24+
}
25+
return globalConfig;
26+
}
27+
28+
export function registerLinters(ctx: ExtensionContext) {
29+
const globalConfig = getGlobalLintConfig();
30+
const linters = new LintCollection(globalConfig, vscode.workspace.getConfiguration("ruby").lint, vscode.workspace.rootPath);
31+
ctx.subscriptions.push(linters);
32+
33+
function executeLinting(e: vscode.TextEditor | vscode.TextDocumentChangeEvent) {
34+
if (!e) return;
35+
linters.run(e.document);
36+
}
37+
38+
// Debounce linting to prevent running on every keypress, only run when typing has stopped
39+
const lintDebounceTime = vscode.workspace.getConfiguration('ruby').lintDebounceTime;
40+
const executeDebouncedLinting = debounce(executeLinting, lintDebounceTime);
41+
42+
ctx.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(executeLinting));
43+
ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument(executeDebouncedLinting));
44+
ctx.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => {
45+
const docs = vscode.window.visibleTextEditors.map(editor => editor.document);
46+
console.log("Config changed. Should lint:", docs.length);
47+
const globalConfig = getGlobalLintConfig();
48+
linters.cfg(vscode.workspace.getConfiguration("ruby").lint, globalConfig);
49+
docs.forEach(doc => linters.run(doc));
50+
}));
51+
52+
// run against all of the current open files
53+
vscode.window.visibleTextEditors.forEach(executeLinting);
54+
}

src/ruby.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ import * as vscode from 'vscode';
44
import { Location, ExtensionContext, Position } from 'vscode';
55
import * as Locate from './locate/locate';
66
import * as path from 'path';
7-
import { LintCollection } from './lint/lintCollection';
87
import * as utils from './utils';
98
import { registerTaskProvider } from './task/rake';
10-
import { Config as LintConfig } from './lint/lintConfig';
11-
import * as debounce from 'lodash/debounce';
129

1310
import { registerCompletionProvider } from './providers/completion';
1411
import { registerFormatter } from './providers/formatter';
1512
import { registerHighlightProvider } from './providers/highlight';
1613
import { registerIntellisenseProvider } from './providers/intellisense';
14+
import { registerLinters } from './providers/linters';
1715

1816
export function activate(context: ExtensionContext) {
1917
const subs = context.subscriptions;
@@ -34,51 +32,3 @@ export function activate(context: ExtensionContext) {
3432
registerTaskProvider(context);
3533
utils.loadEnv();
3634
}
37-
38-
function getGlobalLintConfig() : LintConfig {
39-
let globalConfig = new LintConfig();
40-
41-
let pathToRuby = vscode.workspace.getConfiguration("ruby.interpreter").commandPath;
42-
if (pathToRuby) {
43-
globalConfig.pathToRuby = pathToRuby;
44-
}
45-
46-
let useBundler = vscode.workspace.getConfiguration("ruby").get<boolean | null>("useBundler");
47-
if (useBundler !== null) {
48-
globalConfig.useBundler = useBundler;
49-
}
50-
51-
let pathToBundler = vscode.workspace.getConfiguration("ruby").pathToBundler;
52-
if (pathToBundler) {
53-
globalConfig.pathToBundler = pathToBundler;
54-
}
55-
return globalConfig;
56-
}
57-
58-
function registerLinters(ctx: ExtensionContext) {
59-
const globalConfig = getGlobalLintConfig();
60-
const linters = new LintCollection(globalConfig, vscode.workspace.getConfiguration("ruby").lint, vscode.workspace.rootPath);
61-
ctx.subscriptions.push(linters);
62-
63-
function executeLinting(e: vscode.TextEditor | vscode.TextDocumentChangeEvent) {
64-
if (!e) return;
65-
linters.run(e.document);
66-
}
67-
68-
// Debounce linting to prevent running on every keypress, only run when typing has stopped
69-
const lintDebounceTime = vscode.workspace.getConfiguration('ruby').lintDebounceTime;
70-
const executeDebouncedLinting = debounce(executeLinting, lintDebounceTime);
71-
72-
ctx.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(executeLinting));
73-
ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument(executeDebouncedLinting));
74-
ctx.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => {
75-
const docs = vscode.window.visibleTextEditors.map(editor => editor.document);
76-
console.log("Config changed. Should lint:", docs.length);
77-
const globalConfig = getGlobalLintConfig();
78-
linters.cfg(vscode.workspace.getConfiguration("ruby").lint, globalConfig);
79-
docs.forEach(doc => linters.run(doc));
80-
}));
81-
82-
// run against all of the current open files
83-
vscode.window.visibleTextEditors.forEach(executeLinting);
84-
}

0 commit comments

Comments
 (0)