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

Commit 1a0050a

Browse files
committed
Move completion provider to standalone file
1 parent 6465b49 commit 1a0050a

File tree

2 files changed

+68
-65
lines changed

2 files changed

+68
-65
lines changed

src/providers/completion.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as vscode from 'vscode';
2+
import { ExtensionContext } from 'vscode';
3+
import * as cp from 'child_process';
4+
5+
export function registerCompletionProvider(ctx: ExtensionContext) {
6+
if (vscode.workspace.getConfiguration('ruby').codeCompletion == 'rcodetools') {
7+
const completeCommand = function (args) {
8+
let rctCompletePath = vscode.workspace.getConfiguration('ruby.rctComplete').get('commandPath', 'rct-complete');
9+
args.push('--interpreter');
10+
args.push(vscode.workspace.getConfiguration('ruby.interpreter').get('commandPath', 'ruby'));
11+
if (process.platform === 'win32')
12+
return cp.spawn('cmd', ['/c', rctCompletePath].concat(args));
13+
return cp.spawn(rctCompletePath, args);
14+
}
15+
16+
const completeTest = completeCommand(['--help']);
17+
completeTest.on('exit', () => {
18+
ctx.subscriptions.push(
19+
vscode.languages.registerCompletionItemProvider(
20+
/** selector */'ruby',
21+
/** provider */{
22+
provideCompletionItems: function completionProvider(document, position, token) {
23+
return new Promise((resolve, reject) => {
24+
const line = position.line + 1;
25+
const column = position.character;
26+
let child = completeCommand([
27+
'--completion-class-info',
28+
'--dev',
29+
'--fork',
30+
'--line=' + line,
31+
'--column=' + column
32+
]);
33+
let outbuf = [],
34+
errbuf = [];
35+
child.stderr.on('data', (data) => errbuf.push(data));
36+
child.stdout.on('data', (data) => outbuf.push(data));
37+
child.stdout.on('end', () => {
38+
if (errbuf.length > 0) return reject(Buffer.concat(errbuf).toString());
39+
let completionItems = [];
40+
Buffer.concat(outbuf).toString().split('\n').forEach(function (elem) {
41+
let items = elem.split('\t');
42+
if (/^[^\w]/.test(items[0])) return;
43+
if (items[0].trim().length === 0) return;
44+
let completionItem = new vscode.CompletionItem(items[0]);
45+
completionItem.detail = items[1];
46+
completionItem.documentation = items[1];
47+
completionItem.filterText = items[0];
48+
completionItem.insertText = items[0];
49+
completionItem.label = items[0];
50+
completionItem.kind = vscode.CompletionItemKind.Method;
51+
completionItems.push(completionItem);
52+
}, this);
53+
if (completionItems.length === 0)
54+
return reject([]);
55+
return resolve(completionItems);
56+
});
57+
child.stdin.end(document.getText());
58+
});
59+
}
60+
},
61+
/** triggerCharacters */ ...['.']
62+
)
63+
)
64+
});
65+
completeTest.on('error', () => 0);
66+
}
67+
}

src/ruby.ts

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ 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 * as cp from 'child_process';
87
import { LintCollection } from './lint/lintCollection';
98
import * as utils from './utils';
109
import { registerTaskProvider } from './task/rake';
1110
import { Config as LintConfig } from './lint/lintConfig';
1211
import * as debounce from 'lodash/debounce';
1312

13+
import { registerCompletionProvider } from './providers/completion';
1414
import { registerFormatter } from './providers/formatter';
1515
import { registerHighlightProvider } from './providers/highlight';
1616
import { registerIntellisenseProvider } from './providers/intellisense';
@@ -82,67 +82,3 @@ function registerLinters(ctx: ExtensionContext) {
8282
// run against all of the current open files
8383
vscode.window.visibleTextEditors.forEach(executeLinting);
8484
}
85-
86-
function registerCompletionProvider(ctx: ExtensionContext) {
87-
if (vscode.workspace.getConfiguration('ruby').codeCompletion == 'rcodetools') {
88-
const completeCommand = function (args) {
89-
let rctCompletePath = vscode.workspace.getConfiguration('ruby.rctComplete').get('commandPath', 'rct-complete');
90-
args.push('--interpreter');
91-
args.push(vscode.workspace.getConfiguration('ruby.interpreter').get('commandPath', 'ruby'));
92-
if (process.platform === 'win32')
93-
return cp.spawn('cmd', ['/c', rctCompletePath].concat(args));
94-
return cp.spawn(rctCompletePath, args);
95-
}
96-
97-
const completeTest = completeCommand(['--help']);
98-
completeTest.on('exit', () => {
99-
ctx.subscriptions.push(
100-
vscode.languages.registerCompletionItemProvider(
101-
/** selector */'ruby',
102-
/** provider */{
103-
provideCompletionItems: function completionProvider(document, position, token) {
104-
return new Promise((resolve, reject) => {
105-
const line = position.line + 1;
106-
const column = position.character;
107-
let child = completeCommand([
108-
'--completion-class-info',
109-
'--dev',
110-
'--fork',
111-
'--line=' + line,
112-
'--column=' + column
113-
]);
114-
let outbuf = [],
115-
errbuf = [];
116-
child.stderr.on('data', (data) => errbuf.push(data));
117-
child.stdout.on('data', (data) => outbuf.push(data));
118-
child.stdout.on('end', () => {
119-
if (errbuf.length > 0) return reject(Buffer.concat(errbuf).toString());
120-
let completionItems = [];
121-
Buffer.concat(outbuf).toString().split('\n').forEach(function (elem) {
122-
let items = elem.split('\t');
123-
if (/^[^\w]/.test(items[0])) return;
124-
if (items[0].trim().length === 0) return;
125-
let completionItem = new vscode.CompletionItem(items[0]);
126-
completionItem.detail = items[1];
127-
completionItem.documentation = items[1];
128-
completionItem.filterText = items[0];
129-
completionItem.insertText = items[0];
130-
completionItem.label = items[0];
131-
completionItem.kind = vscode.CompletionItemKind.Method;
132-
completionItems.push(completionItem);
133-
}, this);
134-
if (completionItems.length === 0)
135-
return reject([]);
136-
return resolve(completionItems);
137-
});
138-
child.stdin.end(document.getText());
139-
});
140-
}
141-
},
142-
/** triggerCharacters */ ...['.']
143-
)
144-
)
145-
});
146-
completeTest.on('error', () => 0);
147-
}
148-
}

0 commit comments

Comments
 (0)