Search in sources :

Example 21 with ServerRequestCallback

use of org.rstudio.studio.client.server.ServerRequestCallback in project rstudio by rstudio.

the class CheckSpelling method handleMisspelledWord.

private void handleMisspelledWord(Range range) {
    try {
        docDisplay_.setSelectionRange(range);
        docDisplay_.moveCursorNearTop();
        view_.clearSuggestions();
        view_.getReplacement().setText("");
        final String word = docDisplay_.getTextForRange(range);
        if (changeAll_.containsKey(word)) {
            doReplacement(changeAll_.get(word));
            findNextMisspelling();
            return;
        }
        view_.getMisspelledWord().setText(word);
        // This fixed delay is regrettable but necessary as it can take some
        // time for Ace's scrolling logic to actually execute (i.e. the next
        // time the renderloop runs). If we don't wait, then misspelled words
        // at the end of the document will result in misreported cursor bounds,
        // meaning we'll be avoiding a completely incorrect region.
        Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {

            @Override
            public boolean execute() {
                showDialog(docDisplay_.getCursorBounds());
                view_.focusReplacement();
                spellChecker_.suggestionList(word, new ServerRequestCallback<JsArrayString>() {

                    @Override
                    public void onResponseReceived(JsArrayString response) {
                        String[] suggestions = JsUtil.toStringArray(response);
                        view_.setSuggestions(suggestions);
                        if (suggestions.length > 0) {
                            view_.getReplacement().setText(suggestions[0]);
                            view_.focusReplacement();
                        }
                    }

                    @Override
                    public void onError(ServerError error) {
                        Debug.logError(error);
                    }
                });
                return false;
            }
        }, 100);
    } catch (Exception e) {
        Debug.log(e.toString());
        close();
        RStudioGinjector.INSTANCE.getGlobalDisplay().showErrorMessage("Check Spelling", "An error has occurred:\n\n" + e.getMessage());
        callback_.onFailure(e);
    }
}
Also used : ServerError(org.rstudio.studio.client.server.ServerError) RepeatingCommand(com.google.gwt.core.client.Scheduler.RepeatingCommand) ServerRequestCallback(org.rstudio.studio.client.server.ServerRequestCallback) JsArrayString(com.google.gwt.core.client.JsArrayString) JsArrayString(com.google.gwt.core.client.JsArrayString)

Aggregations

ServerRequestCallback (org.rstudio.studio.client.server.ServerRequestCallback)21 ServerError (org.rstudio.studio.client.server.ServerError)20 ProgressIndicator (org.rstudio.core.client.widget.ProgressIndicator)5 Void (org.rstudio.studio.client.server.Void)5 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)4 Operation (org.rstudio.core.client.widget.Operation)4 VoidServerRequestCallback (org.rstudio.studio.client.server.VoidServerRequestCallback)4 JsArray (com.google.gwt.core.client.JsArray)3 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)3 SourceDocument (org.rstudio.studio.client.workbench.views.source.model.SourceDocument)3 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)2 RepeatingCommand (com.google.gwt.core.client.Scheduler.RepeatingCommand)2 Command (com.google.gwt.user.client.Command)2 Timer (com.google.gwt.user.client.Timer)2 Handler (org.rstudio.core.client.command.Handler)2 ProfileOperationResponse (org.rstudio.studio.client.workbench.views.source.editors.profiler.model.ProfileOperationResponse)2 JsArrayString (com.google.gwt.core.client.JsArrayString)1 Element (com.google.gwt.dom.client.Element)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1