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

Commit 7d9a1f5

Browse files
committed
Move highlightProvider to standalone file
1 parent 0df353f commit 7d9a1f5

File tree

2 files changed

+71
-67
lines changed

2 files changed

+71
-67
lines changed

src/providers/highlight.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as vscode from 'vscode';
2+
import { ExtensionContext } from 'vscode';
3+
4+
export function registerHighlightProvider(ctx: ExtensionContext) {
5+
// highlight provider
6+
let pairedEnds = [];
7+
8+
const getEnd = function (line) {
9+
//end must be on a line by itself, or followed directly by a dot
10+
let match = line.text.match(/^(\s*)end\b[\.\s#]?\s*$/);
11+
if (match) {
12+
return new vscode.Range(line.lineNumber, match[1].length, line.lineNumber, match[1].length + 3);
13+
}
14+
}
15+
16+
const getEntry = function(line) {
17+
let match = line.text.match(/^(.*\b)(begin|class|def|for|if|module|unless|until|case|while)\b[^;]*$/);
18+
if (match) {
19+
return new vscode.Range(line.lineNumber, match[1].length, line.lineNumber, match[1].length + match[2].length);
20+
} else {
21+
//check for do
22+
match = line.text.match(/\b(do)\b\s*(\|.*\|[^;]*)?$/);
23+
if (match) {
24+
return new vscode.Range(line.lineNumber, match.index, line.lineNumber, match.index + 2);
25+
}
26+
}
27+
}
28+
29+
const balancePairs = function (doc) {
30+
pairedEnds = [];
31+
if (doc.languageId !== 'ruby') return;
32+
33+
let waitingEntries = [];
34+
let entry, end;
35+
for (let i = 0; i < doc.lineCount; i++) {
36+
if ((entry = getEntry(doc.lineAt(i)))) {
37+
waitingEntries.push(entry);
38+
} else if (waitingEntries.length && (end = getEnd(doc.lineAt(i)))) {
39+
pairedEnds.push({
40+
entry: waitingEntries.pop(),
41+
end: end
42+
});
43+
}
44+
}
45+
}
46+
47+
const balanceEvent = function (event) {
48+
if (event && event.document) balancePairs(event.document);
49+
}
50+
51+
ctx.subscriptions.push(vscode.languages.registerDocumentHighlightProvider('ruby', {
52+
provideDocumentHighlights: (doc, pos) => {
53+
let result = pairedEnds.find(pair => (
54+
pair.entry.start.line === pos.line ||
55+
pair.end.start.line === pos.line));
56+
if (result) {
57+
return [new vscode.DocumentHighlight(result.entry, 2), new vscode.DocumentHighlight(result.end, 2)];
58+
}
59+
}
60+
}));
61+
62+
ctx.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(balanceEvent));
63+
ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument(balanceEvent));
64+
ctx.subscriptions.push(vscode.workspace.onDidOpenTextDocument(balancePairs));
65+
if (vscode.window && vscode.window.activeTextEditor) {
66+
balancePairs(vscode.window.activeTextEditor.document);
67+
}
68+
}

src/ruby.ts

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { registerTaskProvider } from './task/rake';
1212
import { Config as LintConfig } from './lint/lintConfig';
1313
import * as debounce from 'lodash/debounce';
1414

15+
import { registerHighlightProvider } from './providers/highlight';
16+
1517
export function activate(context: ExtensionContext) {
1618
const subs = context.subscriptions;
1719
// register language config
@@ -52,72 +54,6 @@ function getGlobalLintConfig() : LintConfig {
5254
return globalConfig;
5355
}
5456

55-
function registerHighlightProvider(ctx: ExtensionContext) {
56-
// highlight provider
57-
let pairedEnds = [];
58-
59-
const getEnd = function (line) {
60-
//end must be on a line by itself, or followed directly by a dot
61-
let match = line.text.match(/^(\s*)end\b[\.\s#]?\s*$/);
62-
if (match) {
63-
return new vscode.Range(line.lineNumber, match[1].length, line.lineNumber, match[1].length + 3);
64-
}
65-
}
66-
67-
const getEntry = function(line) {
68-
let match = line.text.match(/^(.*\b)(begin|class|def|for|if|module|unless|until|case|while)\b[^;]*$/);
69-
if (match) {
70-
return new vscode.Range(line.lineNumber, match[1].length, line.lineNumber, match[1].length + match[2].length);
71-
} else {
72-
//check for do
73-
match = line.text.match(/\b(do)\b\s*(\|.*\|[^;]*)?$/);
74-
if (match) {
75-
return new vscode.Range(line.lineNumber, match.index, line.lineNumber, match.index + 2);
76-
}
77-
}
78-
}
79-
80-
const balancePairs = function (doc) {
81-
pairedEnds = [];
82-
if (doc.languageId !== 'ruby') return;
83-
84-
let waitingEntries = [];
85-
let entry, end;
86-
for (let i = 0; i < doc.lineCount; i++) {
87-
if ((entry = getEntry(doc.lineAt(i)))) {
88-
waitingEntries.push(entry);
89-
} else if (waitingEntries.length && (end = getEnd(doc.lineAt(i)))) {
90-
pairedEnds.push({
91-
entry: waitingEntries.pop(),
92-
end: end
93-
});
94-
}
95-
}
96-
}
97-
98-
const balanceEvent = function (event) {
99-
if (event && event.document) balancePairs(event.document);
100-
}
101-
102-
ctx.subscriptions.push(vscode.languages.registerDocumentHighlightProvider('ruby', {
103-
provideDocumentHighlights: (doc, pos) => {
104-
let result = pairedEnds.find(pair => (
105-
pair.entry.start.line === pos.line ||
106-
pair.end.start.line === pos.line));
107-
if (result) {
108-
return [new vscode.DocumentHighlight(result.entry, 2), new vscode.DocumentHighlight(result.end, 2)];
109-
}
110-
}
111-
}));
112-
113-
ctx.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(balanceEvent));
114-
ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument(balanceEvent));
115-
ctx.subscriptions.push(vscode.workspace.onDidOpenTextDocument(balancePairs));
116-
if (vscode.window && vscode.window.activeTextEditor) {
117-
balancePairs(vscode.window.activeTextEditor.document);
118-
}
119-
}
120-
12157
function registerLinters(ctx: ExtensionContext) {
12258
const globalConfig = getGlobalLintConfig();
12359
const linters = new LintCollection(globalConfig, vscode.workspace.getConfiguration("ruby").lint, vscode.workspace.rootPath);
@@ -280,4 +216,4 @@ function registerIntellisenseProvider(ctx: ExtensionContext) {
280216
};
281217
ctx.subscriptions.push(vscode.commands.registerCommand('ruby.reloadProject', rubyLocateDisabled));
282218
}
283-
}
219+
}

0 commit comments

Comments
 (0)