faster completion

cvs
Slava Pestov 2005-03-27 23:17:29 +00:00
parent 0a8e84cd5b
commit bbc7c97aa4
7 changed files with 90 additions and 101 deletions

View File

@ -252,13 +252,16 @@ public class ExternalFactor extends DefaultVocabularyLookup
/* We can't send words across the socket at this point in /* We can't send words across the socket at this point in
human history, because of USE: issues. so we send name/vocab human history, because of USE: issues. so we send name/vocab
pairs. */ pairs. */
Cons moreCompletions = (Cons)parseObject(eval(
String result = eval(
FactorReader.unparseObject(word) FactorReader.unparseObject(word)
+ " " + " "
+ FactorReader.unparseObject(Boolean.valueOf(anywhere)) + FactorReader.unparseObject(Boolean.valueOf(anywhere))
+ " " + " "
+ FactorReader.unparseObject(use) + FactorReader.unparseObject(use)
+ " completions .")).car; + " completions .");
Cons moreCompletions = (Cons)parseObject(result).car;
while(moreCompletions != null) while(moreCompletions != null)
{ {

View File

@ -43,12 +43,10 @@ public abstract class AbstractCompletion extends SideKickCompletion
protected FactorParsedData data; protected FactorParsedData data;
//{{{ AbstractCompletion constructor //{{{ AbstractCompletion constructor
public AbstractCompletion(View view, Object[] items, public AbstractCompletion(View view, FactorParsedData data)
FactorParsedData data)
{ {
this.view = view; this.view = view;
textArea = view.getTextArea(); textArea = view.getTextArea();
this.items = Arrays.asList(items);
this.data = data; this.data = data;
} //}}} } //}}}
@ -58,6 +56,16 @@ public abstract class AbstractCompletion extends SideKickCompletion
return MiscUtilities.getLongestPrefix(items,false); return MiscUtilities.getLongestPrefix(items,false);
} //}}} } //}}}
//{{{ updateInPlace() method
/**
* @return If this returns false, then we create a new completion
* object after user input.
*/
public boolean updateInPlace(EditPane editPane, int caret)
{
return false;
} //}}}
//{{{ handleKeystroke() method //{{{ handleKeystroke() method
public boolean handleKeystroke(int selectedIndex, char keyChar) public boolean handleKeystroke(int selectedIndex, char keyChar)
{ {

View File

@ -2,7 +2,7 @@
plugin.factor.jedit.FactorPlugin.activate=startup plugin.factor.jedit.FactorPlugin.activate=startup
plugin.factor.jedit.FactorPlugin.name=Factor plugin.factor.jedit.FactorPlugin.name=Factor
plugin.factor.jedit.FactorPlugin.version=0.73 plugin.factor.jedit.FactorPlugin.version=0.74
plugin.factor.jedit.FactorPlugin.author=Slava Pestov plugin.factor.jedit.FactorPlugin.author=Slava Pestov
plugin.factor.jedit.FactorPlugin.docs=/doc/jedit/index.html plugin.factor.jedit.FactorPlugin.docs=/doc/jedit/index.html
@ -57,11 +57,11 @@ factor-restart.label=Restart Factor
sidekick.parser.factor.label=Factor sidekick.parser.factor.label=Factor
mode.factor.sidekick.parser=factor mode.factor.sidekick.parser=factor
factor.completion.in=<font color="#a0a0a0">IN: {0}</font>\ factor.completion.in=IN: {0}\
factor.completion.def={0} <b>{1}</b> factor.completion.def={0} {1}
factor.completion.stack={0} ( {1}) factor.completion.stack={0} ( {1})
factor.completion.method=<html>M: <b>{0} {1}</b> factor.completion.method=M: {0} {1}
factor.completion.constructor=<html>C: <b>{0}</b> factor.completion.constructor=C: {0}
# Dialog boxes # Dialog boxes
factor.status.inserted-use=Using {0} factor.status.inserted-use=Using {0}

View File

@ -214,7 +214,7 @@ public class FactorSideKickParser extends SideKickParser
} //}}} } //}}}
//{{{ isWhitespace() method //{{{ isWhitespace() method
private boolean isWhitespace(char ch) private static boolean isWhitespace(char ch)
{ {
return (ReadTable.DEFAULT_READTABLE.getCharacterType(ch) return (ReadTable.DEFAULT_READTABLE.getCharacterType(ch)
== ReadTable.WHITESPACE); == ReadTable.WHITESPACE);
@ -233,6 +233,28 @@ public class FactorSideKickParser extends SideKickParser
return false; return false;
} //}}} } //}}}
//{{{ getCompletionWord() method
public static String getCompletionWord(EditPane editPane, int caret)
{
Buffer buffer = editPane.getBuffer();
int caretLine = buffer.getLineOfOffset(caret);
int lineStart = buffer.getLineStartOffset(caretLine);
String text = buffer.getText(lineStart,caret - lineStart);
int wordStart = 0;
for(int i = text.length() - 1; i >= 0; i--)
{
char ch = text.charAt(i);
if(isWhitespace(ch))
{
wordStart = i + 1;
break;
}
}
return text.substring(wordStart);
} //}}}
//{{{ complete() method //{{{ complete() method
/** /**
* Returns completions suitable for insertion at the specified position. * Returns completions suitable for insertion at the specified position.
@ -255,74 +277,17 @@ public class FactorSideKickParser extends SideKickParser
if(ruleset == null) if(ruleset == null)
return null; return null;
Buffer buffer = editPane.getBuffer(); String word = getCompletionWord(editPane,caret);
// first, we get the word before the caret
int caretLine = buffer.getLineOfOffset(caret);
int lineStart = buffer.getLineStartOffset(caretLine);
String text = buffer.getText(lineStart,caret - lineStart);
/* Don't complete in the middle of a word */
/* int lineEnd = buffer.getLineEndOffset(caretLine) - 1;
if(caret != lineEnd)
{
String end = buffer.getText(caret,lineEnd - caret);
if(!isWhitespace(end.charAt(0)))
return null;
} */
int wordStart = 0;
for(int i = text.length() - 1; i >= 0; i--)
{
char ch = text.charAt(i);
if(isWhitespace(ch))
{
wordStart = i + 1;
break;
}
}
String word = text.substring(wordStart);
/* Don't complete empty string */ /* Don't complete empty string */
if(word.length() == 0) if(word.length() == 0)
return null; return null;
View view = editPane.getView();
if(ruleset.equals("factor::USING")) if(ruleset.equals("factor::USING"))
return vocabComplete(editPane,data,word,caret); return new FactorVocabCompletion(view,word,data);
else else
return wordComplete(editPane,data,word,caret); return new FactorWordCompletion(view,word,data);
} //}}}
//{{{ vocabComplete() method
private SideKickCompletion vocabComplete(EditPane editPane,
FactorParsedData data, String vocab, int caret)
{
String[] completions = FactorPlugin.getVocabCompletions(
vocab,false);
if(completions.length == 0)
return null;
else
{
return new FactorVocabCompletion(editPane.getView(),
completions,vocab,data);
}
} //}}}
//{{{ wordComplete() method
private SideKickCompletion wordComplete(EditPane editPane,
FactorParsedData data, String word, int caret)
{
FactorWord[] completions = FactorPlugin.toWordArray(
FactorPlugin.getWordCompletions(word,false));
if(completions.length == 0)
return null;
else
{
return new FactorWordCompletion(editPane.getView(),
completions,word,data);
}
} //}}} } //}}}
} }

View File

@ -41,10 +41,12 @@ public class FactorVocabCompletion extends AbstractCompletion
private String vocab; private String vocab;
//{{{ FactorVocabCompletion constructor //{{{ FactorVocabCompletion constructor
public FactorVocabCompletion(View view, String[] items, public FactorVocabCompletion(View view, String vocab, FactorParsedData data)
String vocab, FactorParsedData data)
{ {
super(view,items,data); super(view,data);
String[] completions = FactorPlugin.getVocabCompletions(
vocab,false);
this.items = Arrays.asList(completions);
this.vocab = vocab; this.vocab = vocab;
} //}}} } //}}}
@ -87,9 +89,4 @@ public class FactorVocabCompletion extends AbstractCompletion
return true; return true;
} }
} }
public ListCellRenderer getRenderer()
{
return new DefaultListCellRenderer();
}
} }

View File

@ -41,13 +41,39 @@ public class FactorWordCompletion extends AbstractCompletion
private String word; private String word;
//{{{ FactorWordCompletion constructor //{{{ FactorWordCompletion constructor
public FactorWordCompletion(View view, FactorWord[] items, public FactorWordCompletion(View view, String word, FactorParsedData data)
String word, FactorParsedData data)
{ {
super(view,items,data); super(view,data);
FactorWord[] completions = FactorPlugin.toWordArray(
FactorPlugin.getWordCompletions(word,false));
this.items = Arrays.asList(completions);
this.word = word; this.word = word;
} //}}} } //}}}
/**
* @return If this returns false, then we create a new completion
* object after user input.
*/
public boolean updateInPlace(EditPane editPane, int caret)
{
String word = FactorSideKickParser.getCompletionWord(editPane,caret);
List newItems = new ArrayList();
Iterator iter = items.iterator();
while(iter.hasNext())
{
FactorWord w = (FactorWord)iter.next();
if(w.name.startsWith(word))
newItems.add(w);
}
items = newItems;
return true;
}
public void insert(int index) public void insert(int index)
{ {
FactorWord selected = ((FactorWord)get(index)); FactorWord selected = ((FactorWord)get(index));
@ -58,7 +84,6 @@ public class FactorWordCompletion extends AbstractCompletion
try try
{ {
buffer.beginCompoundEdit(); buffer.beginCompoundEdit();
textArea.setSelectedText(insert); textArea.setSelectedText(insert);
if(!FactorPlugin.isUsed(view,selected.vocabulary)) if(!FactorPlugin.isUsed(view,selected.vocabulary))
FactorPlugin.insertUse(view,selected.vocabulary); FactorPlugin.insertUse(view,selected.vocabulary);

View File

@ -39,33 +39,24 @@ public class FactorWordRenderer extends DefaultListCellRenderer
//{{{ getWordHTMLString() method //{{{ getWordHTMLString() method
public static String getWordHTMLString(FactorWord word, boolean showIn) public static String getWordHTMLString(FactorWord word, boolean showIn)
{ {
String defStr = jEdit.getProperty( String str = jEdit.getProperty("factor.completion.def",
"factor.completion.def", new String[] { word.getDefiner().name,word.name });
new String[] {
MiscUtilities.charsToEntities(word.getDefiner().name),
MiscUtilities.charsToEntities(word.name)
});
String in;
if(showIn) if(showIn)
{ {
in = jEdit.getProperty("factor.completion.in", str = jEdit.getProperty("factor.completion.in",
new Object[] { new Object[] {
MiscUtilities.charsToEntities(word.vocabulary) MiscUtilities.charsToEntities(word.vocabulary)
}); }) + str;
} }
else
in = "";
String html = "<html>" + in + defStr;
if(word.stackEffect != null) if(word.stackEffect != null)
{ {
html = jEdit.getProperty("factor.completion.stack", str = jEdit.getProperty("factor.completion.stack",
new String[] { html, word.stackEffect }); new String[] { str, word.stackEffect });
} }
return html; return str;
} //}}} } //}}}
private FactorSideKickParser parser; private FactorSideKickParser parser;