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

Run selected text if available instead of complete exercise code #514

Merged
merged 3 commits into from
Apr 15, 2021

Conversation

gadenbuie
Copy link
Member

Fixes #512.

A note about implementation: I used editor.getSelectedText() to get the selected text in the editor. We're currently using editor.getSession().getValue() for the full code in the editor. I'm fairly certain that editor.getSession() is not required — I think editor.getValue() will return the same thing — and if it is required then we would need to follow a much more complicated way of getting the selected text.

TODO:

  • NEWS item

@schloerke
Copy link
Collaborator

https://stackoverflow.com/a/23996816/591574 suggests something like:

editor.getSession().getTextRange(editor.getSession().selection.getRange())

We do not have multi select mode enabled, so editor.getSelectedText() eventually calls:

 this.session.getTextRange(this.getSelectionRange());

... which is equivalent to editor.getSession().getTextRange(editor.getSession().selection.getRange())


tl/dr; Use editor.getSelectedText()

@schloerke
Copy link
Collaborator

I'm fairly certain that editor.getSession() is not required — I think editor.getValue() will return the same thing

Correct. editor.getValue() calls this.session.getValue(). So they are equivalent.

However, editor.setValue(txt) does an extra text selection step after calling this.session.setValue(txt). From the code, editor.setValue(txt) is not the same as editor.getSession().setValue(txt).

editor.setValue:

    this.setValue = functioned(val, cursorPos) {
        this.session.doc.setValue(val);

        if (!cursorPos)
            this.selectAll();
        else if (cursorPos == 1)
            this.navigateFileEnd();
        else if (cursorPos == -1)
            this.navigateFileStart();

        return val;
    };

editor.getSession().setValue:

    this.setValue = function(text) {
        this.doc.setValue(text);
        this.selection.moveTo(0, 0);

        this.$resetRowCache(0);
        this.$deltas = [];
        this.$deltasDoc = [];
        this.$deltasFold = [];
        this.setUndoManager(this.$undoManager);
        this.getUndoManager().reset();
    };

@gadenbuie gadenbuie merged commit 428d9af into rstudio:master Apr 15, 2021
@gadenbuie gadenbuie deleted the feature/run-selected-text branch April 15, 2021 00:27
Comment on lines +1372 to +1374
value.code = value.should_check
? editor.getSession().getValue()
: editor.getSelectedText() || editor.getSession().getValue();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Run only selected lines of an exercise chunk
2 participants