Skip to content
Permalink
Browse files

realtime spelling: set popup z-index, add more domain specific words,…

… refactor and amend elements that shouldnt be spell checked, moderate performance tweak
  • Loading branch information...
adamconroy committed Aug 20, 2019
1 parent fb1b9fb commit 7fce0110e0f4a29c1c69b519abf41f88a0b35299
@@ -28,6 +28,7 @@
.themedPopupPanel {
margin: -6px;
margin-top: 0px;
z-index: 99999;
}

@sprite .themedPopupPanel .popupTopLeft, .menuPopupTopLeft, .suggestPopupTopLeft {
@@ -33,13 +33,18 @@
import org.rstudio.core.client.StringUtil;
import org.rstudio.core.client.jsonrpc.RequestLog;
import org.rstudio.core.client.jsonrpc.RequestLogEntry;
import org.rstudio.core.client.regex.Pattern;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.common.spelling.model.SpellCheckerResult;
import org.rstudio.studio.client.server.ServerRequestCallback;
import org.rstudio.studio.client.workbench.WorkbenchList;
import org.rstudio.studio.client.workbench.WorkbenchListManager;
import org.rstudio.studio.client.workbench.events.ListChangedEvent;
import org.rstudio.studio.client.workbench.prefs.model.UserPrefs;
import org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor;
import org.rstudio.studio.client.workbench.views.source.editors.text.DocDisplay;
import org.rstudio.studio.client.workbench.views.source.editors.text.Scope;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range;

import java.util.ArrayList;
import java.util.HashSet;
@@ -214,9 +219,9 @@ void initialize(SpellingService spellingService, WorkbenchListManager workbenchL
if (domainSpecificWords_.isEmpty())
{
String[] words = RES.domainSpecificWords().getText().split("[\\r\\n]+");
for (String w : words) {
for (String w : words){
if (w.length() > 0)
domainSpecificWords_.add(w);
domainSpecificWords_.add(w.toLowerCase());
}
}
loadDictionary();
@@ -227,7 +232,7 @@ void initialize(SpellingService spellingService, WorkbenchListManager workbenchL
// word is deemed correct by the dictionary
public boolean checkSpelling(String word)
{
return domainSpecificWords_.contains(word) || allIgnoredWords_.contains(word) || typoNative_.check(word);
return domainSpecificWords_.contains(word.toLowerCase()) || allIgnoredWords_.contains(word) || typoNative_.check(word);
}

public void checkSpelling(List<String> words, final ServerRequestCallback<SpellCheckerResult> callback)
@@ -358,6 +363,27 @@ public void prefetchWords(ArrayList<String> words)
}
}

public boolean shouldCheckSpelling(DocDisplay dd, Range r)
{
String word = dd.getTextForRange(r);
// Don't worry about pathologically long words
if (r.getEnd().getColumn() - r.getStart().getColumn() > 250)
return false;

// Don't spellcheck yaml
Scope s = ((AceEditor)dd).getScopeAtPosition(r.getStart());
if (s.isYaml())
return false;

// Don't spellcheck untokenized words inside a keyword scope
String line = dd.getLine(r.getStart().getRow());
Pattern p = Pattern.create("\\\\(?:documentclass|usepackage|newcounter|setcounter|addtocounter|value|arabic|stepcounter|newenvironment|renewenvironment|ref|vref|eqref|pageref|label|cite[a-zA-Z]*|tag|begin|end|bibitem).*{.*" + word + ".*}");
if (p.test(line))
return false;

return true;
}

public static boolean isLoaded() { return typoLoaded_; }

private final Context context_;
@@ -68,6 +68,7 @@ config
cpp
cran
csl
css
csv
ctrl
cy
@@ -222,6 +223,7 @@ makefiles
mandreyel
mathfont
md
metadata
microtype
minimise
mio
@@ -366,6 +368,7 @@ stdout
strftime
stringi
styler
stylesheet
subcommand
subprocess
subprocesses
@@ -379,6 +382,7 @@ sw
symlink
symlinks
synchronised
sys
templated
templating
testthat
@@ -415,6 +419,7 @@ user
usethis
usethis's
ushey's
utf
valgrind
vctrs
vectorised
@@ -81,44 +81,39 @@ public TextEditingTargetSpelling(DocDisplay docDisplay,
Position.create(docDisplay_.getFirstVisibleRow(), 0),
Position.create(docDisplay_.getLastVisibleRow(), docDisplay_.getLength(docDisplay_.getLastVisibleRow())));

final ArrayList<String> words = new ArrayList<>();
final ArrayList<Range> wordRanges = new ArrayList<>();
JsArray<LintItem> lint = JsArray.createArray().cast();
ArrayList<String> prefetchWords = new ArrayList<>();

for (Range r : wordSource)
{
// Don't worry about pathologically long words
if (r.getEnd().getColumn() - r.getStart().getColumn() > 250)
if (!typoSpellChecker_.shouldCheckSpelling(docDisplay_, r))
continue;

wordRanges.add(r);
words.add(docDisplay_.getTextForRange(r));

// only check a certain number of words at once to not overwhelm the system
if (wordRanges.size() > prefs_.maxSpellcheckWords().getValue())
break;
}

JsArray<LintItem> lint = JsArray.createArray().cast();
if (wordRanges.size() > 0)
{
ArrayList<String> prefetchWords = new ArrayList<>();
for (int i = 0; i < words.size(); i++) {
String word = words.get(i);
if (!typoSpellChecker_.checkSpelling(word)) {
if (prefetchWords.size() < prefs_.maxSpellcheckPrefetch().getValue())
prefetchWords.add(word);
Range range = wordRanges.get(i);
lint.push(LintItem.create(
range.getStart().getRow(),
range.getStart().getColumn(),
range.getEnd().getRow(),
range.getEnd().getColumn(),
"Spellcheck",
"spelling"));
}
String word = docDisplay_.getTextForRange(r);
if (!typoSpellChecker_.checkSpelling(word)) {
if (prefetchWords.size() < prefs_.maxSpellcheckPrefetch().getValue())
prefetchWords.add(word);

lint.push(LintItem.create(
r.getStart().getRow(),
r.getStart().getColumn(),
r.getEnd().getRow(),
r.getEnd().getColumn(),
"Spellcheck",
"spelling"));
}
typoSpellChecker_.prefetchWords(prefetchWords);
}

if (prefetchWords.size() > 0)
typoSpellChecker_.prefetchWords(prefetchWords);

return lint;
}

@@ -81,7 +81,7 @@ public CheckSpelling(TypoSpellChecker spellChecker,
ProgressDisplay progressDisplay,
ResultCallback<Void, Exception> callback)
{
spellChecker_ = spellChecker;
typoSpellChecker_ = spellChecker;
docDisplay_ = docDisplay;
view_ = view;
progressDisplay_ = progressDisplay;
@@ -116,14 +116,14 @@ public CheckSpelling(TypoSpellChecker spellChecker,

view_.getIgnoreAllButton().addClickHandler((ClickEvent event) ->
{
spellChecker_.addIgnoredWord(view_.getMisspelledWord().getText());
typoSpellChecker_.addIgnoredWord(view_.getMisspelledWord().getText());
currentPos_ = docDisplay_.getCursorPosition();
findNextMisspelling();
});

view_.getAddButton().addClickHandler((ClickEvent event) ->
{
spellChecker_.addToUserDictionary(view_.getMisspelledWord().getText());
typoSpellChecker_.addToUserDictionary(view_.getMisspelledWord().getText());
currentPos_ = docDisplay_.getCursorPosition();
findNextMisspelling();
});
@@ -178,8 +178,7 @@ private void findNextMisspelling()

for (Range r : wordSource)
{
// Don't worry about pathologically long words
if (r.getEnd().getColumn() - r.getStart().getColumn() > 250)
if (!typoSpellChecker_.shouldCheckSpelling(docDisplay_, r))
continue;

wordRanges.add(r);
@@ -196,7 +195,7 @@ private void findNextMisspelling()

if (wordRanges.size() > 0)
{
spellChecker_.checkSpelling(words, new SimpleRequestCallback<SpellCheckerResult>()
typoSpellChecker_.checkSpelling(words, new SimpleRequestCallback<SpellCheckerResult>()
{
@Override
public void onResponseReceived(SpellCheckerResult response)
@@ -309,7 +308,7 @@ private void handleMisspelledWord(Range range)

view_.focusReplacement();

String[] suggestions = spellChecker_.suggestionList(word);
String[] suggestions = typoSpellChecker_.suggestionList(word);
view_.setSuggestions(suggestions);
if (suggestions.length > 0)
{
@@ -331,7 +330,7 @@ private void handleMisspelledWord(Range range)
}
}

private final TypoSpellChecker spellChecker_;
private final TypoSpellChecker typoSpellChecker_;
private final DocDisplay docDisplay_;
private final Display view_;
private final ProgressDisplay progressDisplay_;

0 comments on commit 7fce011

Please sign in to comment.
You can’t perform that action at this time.