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

Add completion feature #41

Merged
merged 11 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"dependencies": {
"vscode-debugprotocol": "~1.6.0-pre4",
"vscode-debugadapter": "~1.6.0-pre8",
"xmldom": "^0.1.19"
"xmldom": "^0.1.19",
"which": "^1.2.10"
},
"devDependencies": {
"gulp": "^3.9.0",
Expand Down
58 changes: 58 additions & 0 deletions ruby.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use strict";
let vscode = require('vscode');
let linters = require('./lint/lint');
let cp = require('child_process');
let which = require('which');

const severities = {
refactor: vscode.DiagnosticSeverity.Hint,
convention: vscode.DiagnosticSeverity.Information,
Expand Down Expand Up @@ -104,6 +107,54 @@ function balanceEvent(event) {
if (event && event.document) balancePairs(event.document);
}

function completionProvider(document, position, token) {
return new Promise((resolve, reject) => {
const line = position.line + 1;
const column = position.character;
console.log("line=" , line);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may want to remove this.

console.log("column=" , column);

var sh, flag;
if (process.platform === 'win32') {
sh = 'cmd'; flag = '/c';
} else {
sh = '/bin/sh'; flag = '-c';
}
var child = cp.spawn(sh, [
flag,
'rct-complete',
'--completion-class-info',
'--dev',
'--fork',
'--line='+line,
'--column='+column], { detached: false });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detached: false is the default.

child.stderr.on('data', (data) => {
console.log("stderr:", data.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop this. If it's not reported to the user interface, there's no need to report it.

reject(data);
Copy link
Contributor

@HookyQR HookyQR Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to child.stderr.on('end', ... instead. Avoids double calling reject. (It's unlikely to happen, but possible)

Actually, this listener can be removed altogether. child.stdout.on('end', ... will be called even if no data was sent to the stream.

});
child.stdout.on('data', (data) => {
console.log("stdout:", data.toString());
var completionItems = [];
Copy link
Contributor

@HookyQR HookyQR Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collect the completionItems in the scope, but handle the response in .on('end' instead.

My preference is to collect the buffers in an array, then concat them together afterwards. Saves some memory allocation calls.

data.toString().split('\n').forEach(function(elem) {
var items = elem.split('\t');
if (items.length != 2) return;
var completionItem = new vscode.CompletionItem(items[0]);
completionItem.detail = items[1];
completionItem.documentation = items[1];
completionItem.filterText = items[1];
completionItem.insertText = items[0];
completionItem.label = items[0];
completionItems.push(completionItem);
}, this);
if (completionItems.length == 0)
return reject([]);
return resolve(completionItems);
});
child.stdin.write(document.getText());
child.stdin.end();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collapse these calls into child.stdin.end(document.getText()).

});
}

function activate(context) {
//add language config
vscode.languages.setLanguageConfiguration('ruby', langConfig);
Expand Down Expand Up @@ -157,6 +208,13 @@ function activate(context) {
balancePairs(vscode.window.activeTextEditor.document);
}

which("rct-complete", function(err) {
if (err == null) {
vscode.languages.registerCompletionItemProvider('ruby', {
provideCompletionItems: completionProvider
});
}
});
}

exports.activate = activate;
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RubyDebugSession extends DebugSession {
private _variableHandles: Handles<IDebugVariable>;
private rubyProcess: RubyProcess;
private requestArguments: any;
private debugMode: Mode;
private debugMode: Mode;

/**
* Creates a new debug adapter.
Expand Down