Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab key will open and autocomplete code #428

Merged
merged 4 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ learnr (development version)
* Added error handling when user specifies a non-existent label for `exercise.setup` option with an error message. ([#390](https://github.com/rstudio/learnr/pull/390))
* We no longer forward the checker code to browser (in html), but instead cache it. ([#390](https://github.com/rstudio/learnr/pull/390))
* We no longer display an invisible exercise result warning automatically. Instead, authors must set the exercise chunk option `exercise.warn_invisible = TRUE` to display an invisible result warning message. ([#373](https://github.com/rstudio/learnr/pull/373))
* Hitting the `TAB` key in an exercise has always opened the auto-completion drop down. Now, hitting the `TAB` key will also complete the currently selected code completion. ([#428](https://github.com/rstudio/learnr/pull/428))

## Bug fixes

Expand Down
37 changes: 22 additions & 15 deletions inst/lib/tutorial/tutorial-autocompletion.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function TutorialCompleter(tutorial) {
var lines = data.lines || [];
if (lines.length !== 1)
return;

// NOTE: Ace has already updated the document line at this point
// so we can just look at the state of that line
var pos = this.getCursorPosition();
Expand All @@ -37,7 +37,7 @@ function TutorialCompleter(tutorial) {

this.$autocompletionTimerId = setTimeout(this.$liveAutocompleter, delayMs);
};

var MODIFIER_NONE = 0;
var MODIFIER_CTRL = 1;
var MODIFIER_ALT = 2;
Expand Down Expand Up @@ -74,12 +74,12 @@ function TutorialCompleter(tutorial) {
}

function initializeCompletionEngine(editor) {

editor.completers = editor.completers || [];
editor.completers.push({

getCompletions: function(editor, session, position, prefix, callback) {

// send autocompletion request with document contents up to cursor
// position (done to enable multi-line autocompletions)
var contents = session.getTextRange({
Expand All @@ -93,7 +93,7 @@ function TutorialCompleter(tutorial) {
};

self.$tutorial.$serverRequest("completion", payload, function(data) {

data = data || [];

// define a custom completer -- used for e.g. automatic
Expand All @@ -109,7 +109,7 @@ function TutorialCompleter(tutorial) {
ranges[i].start.column -= n;
editor.session.remove(ranges[i]);
}

// insert completion term (add parentheses for functions)
var term = data.value + (data.is_function ? "()" : "");
editor.execCommand("insertstring", term);
Expand Down Expand Up @@ -149,18 +149,18 @@ function TutorialCompleter(tutorial) {
}

var ensureInitialized = function(editor) {

// bail if completions are disabled for this editor
if (!editor.tutorial.completion)
return;

if (editor.$autocompletionInitialized)
return;

initializeAceEventListeners(editor);
initializeCompletionEngine(editor);
initializeSetupChunk(editor);

// generate a live autocompleter for this editor if
// not yet available
if (typeof editor.$liveAutocompleter === "undefined") {
Expand Down Expand Up @@ -188,11 +188,11 @@ function TutorialCompleter(tutorial) {
var editor = findActiveAceInstance();
if (editor == null)
return;

// bail if completions are disabled for this editor
if (!editor.tutorial.completion)
return;

// ensure completion engine initialized
ensureInitialized(editor);

Expand All @@ -217,12 +217,19 @@ function TutorialCompleter(tutorial) {
return;

var keys = new KeyCombination(event);
if (keys.keyCode == KEYCODE_TAB && keys.modifier == MODIFIER_NONE)
return autocomplete();

if (keys.keyCode == KEYCODE_TAB && keys.modifier == MODIFIER_NONE) {
if (editor && editor.completer && editor.completer.activated) {
// it is already activated. Accept the top choice. To do this, do nothing and it will be resolved by the autocompleter
//// do nothing. let autocompleter handle it
} else {
// autocompleter is not active. enable it
return autocomplete()
}
cpsievert marked this conversation as resolved.
Show resolved Hide resolved
}

if (keys.keyCode == KEYCODE_SPACE && keys.modifier == MODIFIER_CTRL)
return autocomplete();

}, true);

}