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 6 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
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ Settings available (in your VSCode workspace) for each of the linters:
}
```

## Completion

You will need to install the ruby gem for Intellisense/Completion.

* rcodetools

You need to restart vscode after installation of rcodetools. Then type CTRL-Space after the leading words like below.
Copy link
Contributor

@orta orta Aug 5, 2016

Choose a reason for hiding this comment

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

To make this read better in English, maybe try this:

## Autocomplete

To enable method completion in ruby: `gem install rcodetools`. You may need to restart Visual Studio Code the first time.

```ruby
[1, 2, 3].e #<= Press CTRL-Space here

Move the - IntelliSense and autocomplete line from TODO into Features.


```ruby
[1,2,3].e #<= Type CTRL-Space at here
```

## Features

- Ruby scripts debugging
Expand Down
51 changes: 51 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,46 @@ function balanceEvent(event) {
if (event && event.document) balancePairs(event.document);
}

var completeCmd;

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

var child = cp.spawn(completeCmd, [
'--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) => {
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 (/^[^\w]/.test(items[0])) return;
var completionItem = new vscode.CompletionItem(items[0]);
completionItem.detail = items[1];
completionItem.documentation = items[1];
completionItem.filterText = items[0];
completionItem.insertText = items[0];
completionItem.label = items[0];
completionItem.kind = vscode.CompletionItemKind.Method;
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 +200,14 @@ function activate(context) {
balancePairs(vscode.window.activeTextEditor.document);
}

which("rct-complete", function(err, found) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not really a fan of this call because I use rbenv. I'd rather it just assume it is in the path, or have a setting in the users config to point to it.

Also, this may cause a problem if the user installs it for one version of ruby, but the shell that is spawned for which can't find it.

I guess it's ok like this until the issues start coming in. If they don't, then all is well. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your review. I don't like handling completion event even though rct-cmplete is not installed. How about to try to run rct-complete --help at once to check rct-complete is installed?

Copy link
Contributor Author

@mattn mattn Aug 6, 2016

Choose a reason for hiding this comment

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

Ah, I just remember why I did this imlement. cp.spawn doesn't accept only command name on windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I figure out this problem. I'm using msys2 mingw64 environment. And ruby on mingw64 doesn't allow non-absolute path in the command.

if (!err && found) {
completeCmd = found;
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