From a8975900bd1d4023453f27385995affb817f3ab6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 25 Nov 2004 02:45:30 +0000 Subject: [PATCH] ExternalFactor is done --- TODO.FACTOR.txt | 2 +- factor/ExternalFactor.java | 56 +++++++++++++++++++++++++++++++++ factor/jedit/FactorPlugin.java | 42 ++++++------------------- library/tools/word-tools.factor | 5 +++ 4 files changed, 71 insertions(+), 34 deletions(-) diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index 8004ff5419..7d0fcc4873 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -26,12 +26,12 @@ + listener/plugin: +- listener: if too many things popped off the stack, complain - gracefully handle non-working cfactor - don't show listener on certain commands - NPE in ErrorHighlight - some way to not have previous definitions from a source file clutter the namespace -- finish ExternalFactor VocabularyLookup - maple-like: press enter at old commands to evaluate there - completion in the listener - special completion for USE:/IN: diff --git a/factor/ExternalFactor.java b/factor/ExternalFactor.java index 683ae9304d..b053554164 100644 --- a/factor/ExternalFactor.java +++ b/factor/ExternalFactor.java @@ -115,6 +115,29 @@ public class ExternalFactor extends DefaultVocabularyLookup return new FactorStream(client); } //}}} + //{{{ getVocabularies() method + public Cons getVocabularies() + { + Cons vocabs = super.getVocabularies(); + + try + { + Cons moreVocabs = (Cons)parseObject(eval("vocabs.")).car; + while(moreVocabs != null) + { + String vocab = (String)moreVocabs.car; + if(!Cons.contains(vocabs,vocab)) + vocabs = new Cons(vocab,vocabs); + moreVocabs = moreVocabs.next(); + } + } + catch(Exception e) + { + Log.log(Log.ERROR,this,e); + } + return vocabs; + } //}}} + //{{{ searchVocabulary() method /** * Search through the given vocabulary list for the given word. @@ -148,6 +171,39 @@ public class ExternalFactor extends DefaultVocabularyLookup } } //}}} + //{{{ getCompletions() method + public void getCompletions(String vocab, String word, List completions, + boolean anywhere) + { + super.getCompletions(vocab,word,completions,anywhere); + + try + { + /* We can't send words across the socket at this point in + human history, because of USE: issues. so we send name/vocab + pairs. */ + Cons moreCompletions = (Cons)parseObject(eval( + FactorReader.unparseObject(word) + + " " + + FactorReader.unparseObject(vocab) + + " " + + (anywhere ? "vocab-apropos" : "vocab-completions") + + " [ dup word-name swap word-vocabulary 2list ] map .")).car; + + while(moreCompletions != null) + { + Cons completion = (Cons)moreCompletions.car; + completions.add(searchVocabulary(completion.next(), + (String)completion.car)); + moreCompletions = moreCompletions.next(); + } + } + catch(Exception e) + { + Log.log(Log.ERROR,this,e); + } + } //}}} + //{{{ close() method /** * Close communication session. Factor will then exit. diff --git a/factor/jedit/FactorPlugin.java b/factor/jedit/FactorPlugin.java index 28a0d415ff..a25565d03d 100644 --- a/factor/jedit/FactorPlugin.java +++ b/factor/jedit/FactorPlugin.java @@ -220,35 +220,6 @@ public class FactorPlugin extends EditPlugin } } //}}} - //{{{ getCompletions() method - /** - * @param anywhere If true, matches anywhere in the word name are - * returned; otherwise, only matches from beginning. - */ - public static List getCompletions(Iterator use, String word, boolean anywhere) - { - try - { - List completions = new ArrayList(); - - while(use.hasNext()) - { - String vocab = (String)use.next(); - getExternalInstance().getCompletions( - vocab,word,completions,anywhere); - } - - Collections.sort(completions, - new MiscUtilities.StringICaseCompare()); - - return completions; - } - catch(Exception e) - { - throw new RuntimeException(e); - } - } //}}} - //{{{ getCompletions() method /** * @param anywhere If true, matches anywhere in the word name are @@ -358,14 +329,19 @@ public class FactorPlugin extends EditPlugin private static FactorWord[] findAllWordsNamed(View view, String word) throws Exception { + ExternalFactor external = getExternalInstance(); + ArrayList words = new ArrayList(); - Iterator vocabs = getExternalInstance().getVocabularies(); - while(vocabs.hasNext()) + + Cons vocabs = external.getVocabularies(); + while(vocabs != null) { - Map vocab = (Map)vocabs.next(); - FactorWord w = (FactorWord)vocab.get(word); + String vocab = (String)vocabs.car; + FactorWord w = (FactorWord)external.searchVocabulary( + new Cons(vocab,null),word); if(w != null) words.add(w); + vocabs = vocabs.next(); } return (FactorWord[])words.toArray(new FactorWord[words.size()]); } //}}} diff --git a/library/tools/word-tools.factor b/library/tools/word-tools.factor index b5658b48fb..4525eb40fa 100644 --- a/library/tools/word-tools.factor +++ b/library/tools/word-tools.factor @@ -79,6 +79,11 @@ USE: unparser 2drop ] ifte ; +: vocab-completions ( substring vocab -- list ) + #! Used by jEdit plugin. Like vocab-apropos, but only + #! matches at the start of a word name are considered. + words [ word-name over str-head? ] subset nip ; + : apropos. ( substring -- ) #! List all words that contain a string. vocabs [ dupd vocab-apropos. ] each drop ;