Skip to content

Commit

Permalink
Merge pull request #13 from jeffreyhorner/master
Browse files Browse the repository at this point in the history
Spelling Sandbox now operational
  • Loading branch information
jjallaire committed Mar 16, 2012
2 parents fa071d3 + 4c90c3e commit adbc1a2
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 29 deletions.
20 changes: 19 additions & 1 deletion src/cpp/session/modules/SessionSpelling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ Error suggestionList(const json::JsonRpcRequest& request,
return Success();
}

Error addToDictionary(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
std::string word;
Error error = json::readParams(request.params, &word);
if (error)
return error;

bool added;
error = s_pSpellChecker->addWord(word,&added);
if (error)
return error;

pResponse->setResult(added);
return Success();
}


} // anonymous namespace

Expand Down Expand Up @@ -237,7 +254,8 @@ Error initialize()
ExecBlock initBlock ;
initBlock.addFunctions()
(bind(registerRpcMethod, "check_spelling", checkSpelling))
(bind(registerRpcMethod, "suggestion_list", suggestionList));
(bind(registerRpcMethod, "suggestion_list", suggestionList))
(bind(registerRpcMethod, "add_to_dictionary", addToDictionary));
return initBlock.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ void checkSpelling(String word,

void suggestionList(String word,
ServerRequestCallback<JsArrayString> requestCallback);

void addToDictionary(String word,
ServerRequestCallback<Boolean> requestCallback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.rstudio.studio.client.common.spelling.view;

public interface SpellCheckerTarget
{
void changeWord(String fromWord,String toWord);
void changeAllWords(String fromWord,String toWord);
String getNextWord();
boolean isEmpty();
void spellCheckComplete();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* SpellingSandboxDialog.java
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
package org.rstudio.studio.client.common.spelling.view;

import java.util.ArrayList;

import org.rstudio.core.client.Debug;
import com.google.gwt.user.client.ui.TextArea;

public class SpellingSandboxDebugText extends TextArea
implements SpellCheckerTarget
{

public SpellingSandboxDebugText()
{
setCharacterWidth(80);
setVisibleLines(20);
}

public void changeWord(String fromWord,String toWord)
{
if (wordIdx_ > 0 && words_.get(wordIdx_-1).compareTo(fromWord) == 0){
words_.set(wordIdx_-1,toWord);
isDirty_ = true;
}
}

public void changeAllWords(String fromWord,String toWord)
{
for (int i = 0; i < words_.size(); i++)
{
if (words_.get(i).compareTo(fromWord) == 0)
{
words_.set(i, toWord);
isDirty_ = true;
}
}
}

public String getNextWord()
{
if (wordIdx_ < words_.size())
{
return words_.get(wordIdx_++);
} else
{
return "";
}
}
public boolean isEmpty()
{
return getText().trim().isEmpty();
}

public ArrayList<String> stringArrayToList(String[] ary)
{
ArrayList<String> lst = new ArrayList<String>();
if (ary.length > 0)
{
if (!ary[0].isEmpty())
lst.add(ary[0]);
for (int i = 1; i < ary.length; i++)
lst.add(ary[i]);
}
return lst;
}

public void logArrayList(String msg, ArrayList<String> ary)
{
Debug.log(msg+"\n");
for (int i = 0; i < ary.size(); i++)
{
Debug.log("<"+ary.get(i)+">\n");
}
}
public void startSpellChecking()
{
Debug.log("text: <"+getText()+">\n");
words_ = stringArrayToList(getText().split("[^"+wordPattern_+"]+"));
logArrayList("words_:",words_);
nonWords_ = stringArrayToList(getText().split("["+wordPattern_+"]+"));
logArrayList("nonWords_:",nonWords_);
wordIdx_ = 0;
isDirty_ = false;
}

public void spellCheckComplete()
{
if (!isDirty_)
return;

// Replace textarea with current words and nonWords
if (nonWords_.size() > 0)
{
ArrayList<String> ary1, ary2;
if (String.valueOf(getText().charAt(0)).matches("["+wordPattern_+"]"))
{
Debug.log("Printing words first\n");
ary1 = words_;
ary2 = nonWords_;
} else
{
ary1 = nonWords_;
ary2 = words_;
}
int len = (ary1.size() <= ary2.size())? ary1.size() : ary2.size();

StringBuilder replace = new StringBuilder();
for (int i = 0; i < len; i++){
replace.append(ary1.get(i));
replace.append(ary2.get(i));
}
if (ary1.size() <= ary2.size())
{
for (int i = ary1.size(); i < ary2.size(); i++)
replace.append(ary2.get(i));
} else
{
for (int i = ary2.size(); i < ary1.size(); i++)
replace.append(ary1.get(i));

}
setText(replace.toString());
} else {
setText(words_.get(0));
}
}

private boolean isDirty_;
private int wordIdx_;
private String wordPattern_ = "\\w'";
private ArrayList<String> words_;
private ArrayList<String> nonWords_;
}

Loading

0 comments on commit adbc1a2

Please sign in to comment.