Search in sources :

Example 1 with SpellCheckerResult

use of org.rstudio.studio.client.common.spelling.model.SpellCheckerResult in project rstudio by rstudio.

the class SpellChecker method checkSpelling.

public void checkSpelling(List<String> words, final ServerRequestCallback<SpellCheckerResult> callback) {
    // allocate results
    final SpellCheckerResult spellCheckerResult = new SpellCheckerResult();
    if (words.isEmpty()) {
        callback.onResponseReceived(spellCheckerResult);
        return;
    }
    // only send words to the server that aren't ignored
    final ArrayList<String> wordsToCheck = new ArrayList<String>();
    for (int i = 0; i < words.size(); i++) {
        String word = words.get(i);
        if (isWordIgnored(word))
            spellCheckerResult.getCorrect().add(word);
        else
            wordsToCheck.add(word);
    }
    // call the service to check the non-ignored words
    spellingService_.checkSpelling(wordsToCheck, new ServerRequestCallback<SpellCheckerResult>() {

        @Override
        public void onResponseReceived(SpellCheckerResult result) {
            spellCheckerResult.getCorrect().addAll(result.getCorrect());
            spellCheckerResult.getIncorrect().addAll(result.getIncorrect());
            callback.onResponseReceived(spellCheckerResult);
        }

        @Override
        public void onError(ServerError error) {
            callback.onError(error);
        }
    });
}
Also used : ServerError(org.rstudio.studio.client.server.ServerError) SpellCheckerResult(org.rstudio.studio.client.common.spelling.model.SpellCheckerResult) ArrayList(java.util.ArrayList) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 2 with SpellCheckerResult

use of org.rstudio.studio.client.common.spelling.model.SpellCheckerResult in project rstudio by rstudio.

the class SpellingService method checkSpelling.

public void checkSpelling(List<String> words, final ServerRequestCallback<SpellCheckerResult> callback) {
    // results to return
    final SpellCheckerResult spellCheckerResult = new SpellCheckerResult();
    // only send words to the server that aren't in the cache
    final ArrayList<String> wordsToCheck = new ArrayList<String>();
    for (int i = 0; i < words.size(); i++) {
        String word = words.get(i);
        Boolean isCorrect = previousResults_.get(word);
        if (isCorrect != null) {
            if (isCorrect)
                spellCheckerResult.getCorrect().add(word);
            else
                spellCheckerResult.getIncorrect().add(word);
        } else {
            wordsToCheck.add(word);
        }
    }
    // if there are no words to check then return
    if (wordsToCheck.size() == 0) {
        callback.onResponseReceived(spellCheckerResult);
        return;
    }
    // hit the server
    server_.checkSpelling(JsUtil.toJsArrayString(wordsToCheck), new ServerRequestCallback<JsArrayInteger>() {

        @Override
        public void onResponseReceived(JsArrayInteger result) {
            // get misspelled indexes
            ArrayList<Integer> misspelledIndexes = new ArrayList<Integer>();
            for (int i = 0; i < result.length(); i++) misspelledIndexes.add(result.get(i));
            // determine correct/incorrect status and populate result & cache
            for (int i = 0; i < wordsToCheck.size(); i++) {
                String word = wordsToCheck.get(i);
                if (misspelledIndexes.contains(i)) {
                    spellCheckerResult.getIncorrect().add(word);
                    previousResults_.put(word, false);
                } else {
                    spellCheckerResult.getCorrect().add(word);
                    previousResults_.put(word, true);
                }
            }
            // return result
            callback.onResponseReceived(spellCheckerResult);
        }

        @Override
        public void onError(ServerError error) {
            callback.onError(error);
        }
    });
}
Also used : JsArrayInteger(com.google.gwt.core.client.JsArrayInteger) ServerError(org.rstudio.studio.client.server.ServerError) SpellCheckerResult(org.rstudio.studio.client.common.spelling.model.SpellCheckerResult) JsArrayInteger(com.google.gwt.core.client.JsArrayInteger) ArrayList(java.util.ArrayList) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 3 with SpellCheckerResult

use of org.rstudio.studio.client.common.spelling.model.SpellCheckerResult in project rstudio by rstudio.

the class CheckSpelling method findNextMisspelling.

private void findNextMisspelling() {
    try {
        if (checkForCancel())
            return;
        showProgress();
        Iterable<Range> wordSource = docDisplay_.getWords(docDisplay_.getFileType().getTokenPredicate(), docDisplay_.getFileType().getCharPredicate(), currentPos_, wrapped_ ? initialCursorPos_.getPosition() : null);
        final ArrayList<String> words = new ArrayList<String>();
        final ArrayList<Range> wordRanges = new ArrayList<Range>();
        for (Range r : wordSource) {
            // Don't worry about pathologically long words
            if (r.getEnd().getColumn() - r.getStart().getColumn() > 250)
                continue;
            wordRanges.add(r);
            words.add(docDisplay_.getTextForRange(r));
            // Check a maximum of N words at a time
            if (wordRanges.size() == 100)
                break;
        }
        if (wordRanges.size() > 0) {
            spellChecker_.checkSpelling(words, new SimpleRequestCallback<SpellCheckerResult>() {

                @Override
                public void onResponseReceived(SpellCheckerResult response) {
                    if (checkForCancel())
                        return;
                    for (int i = 0; i < words.size(); i++) {
                        if (response.getIncorrect().contains(words.get(i))) {
                            handleMisspelledWord(wordRanges.get(i));
                            return;
                        }
                    }
                    currentPos_ = wordRanges.get(wordRanges.size() - 1).getEnd();
                    // Everything spelled correctly, continue
                    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                        @Override
                        public void execute() {
                            findNextMisspelling();
                        }
                    });
                }
            });
        } else {
            // No misspellings
            if (wrapped_) {
                close();
                RStudioGinjector.INSTANCE.getGlobalDisplay().showMessage(GlobalDisplay.MSG_INFO, "Check Spelling", "Spell check is complete.");
                callback_.onSuccess(Void.create());
            } else {
                wrapped_ = true;
                currentPos_ = Position.create(0, 0);
                findNextMisspelling();
            }
        }
    } 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 : ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) SpellCheckerResult(org.rstudio.studio.client.common.spelling.model.SpellCheckerResult) ArrayList(java.util.ArrayList) JsArrayString(com.google.gwt.core.client.JsArrayString) Range(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)

Aggregations

JsArrayString (com.google.gwt.core.client.JsArrayString)3 ArrayList (java.util.ArrayList)3 SpellCheckerResult (org.rstudio.studio.client.common.spelling.model.SpellCheckerResult)3 ServerError (org.rstudio.studio.client.server.ServerError)2 JsArrayInteger (com.google.gwt.core.client.JsArrayInteger)1 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)1 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)1