hashtable prettyprinting

cvs
Slava Pestov 2004-11-26 01:37:05 +00:00
parent a8975900bd
commit 22dc78433c
9 changed files with 60 additions and 36 deletions

View File

@ -28,7 +28,6 @@
- listener: if too many things popped off the stack, complain - listener: if too many things popped off the stack, complain
- gracefully handle non-working cfactor - gracefully handle non-working cfactor
- don't show listener on certain commands
- NPE in ErrorHighlight - NPE in ErrorHighlight
- some way to not have previous definitions from a source file - some way to not have previous definitions from a source file
clutter the namespace clutter the namespace
@ -64,7 +63,6 @@
objects objects
- worddef props - worddef props
- prettyprint: when unparse called due to recursion, write a link - prettyprint: when unparse called due to recursion, write a link
- prettyprinter should output {{ ... }} syntax for hashtables
- FORGET: and forget - FORGET: and forget
+ httpd: + httpd:

View File

@ -172,7 +172,7 @@ public class ExternalFactor extends DefaultVocabularyLookup
} //}}} } //}}}
//{{{ getCompletions() method //{{{ getCompletions() method
public void getCompletions(String vocab, String word, List completions, public void getCompletions(String vocab, String word, Set completions,
boolean anywhere) boolean anywhere)
{ {
super.getCompletions(vocab,word,completions,anywhere); super.getCompletions(vocab,word,completions,anywhere);
@ -193,8 +193,10 @@ public class ExternalFactor extends DefaultVocabularyLookup
while(moreCompletions != null) while(moreCompletions != null)
{ {
Cons completion = (Cons)moreCompletions.car; Cons completion = (Cons)moreCompletions.car;
completions.add(searchVocabulary(completion.next(), FactorWord w = searchVocabulary(completion.next(),
(String)completion.car)); (String)completion.car);
if(w != null)
completions.add(w);
moreCompletions = moreCompletions.next(); moreCompletions = moreCompletions.next();
} }
} }

View File

@ -117,13 +117,11 @@ public class EditWordDialog extends WordListDialog
//{{{ updateList() method //{{{ updateList() method
private void updateList() private void updateList()
{ {
List completions = FactorPlugin.getCompletions( FactorWord[] completions = FactorPlugin.toWordArray(
field.getText(),true); FactorPlugin.getCompletions(
FactorWord[] completionArray field.getText(),true));
= (FactorWord[])completions.toArray( list.setListData(completions);
new FactorWord[completions.size()]); if(completions.length != 0)
list.setListData(completionArray);
if(completionArray.length != 0)
{ {
list.setSelectedIndex(0); list.setSelectedIndex(0);
list.ensureIndexIsVisible(0); list.ensureIndexIsVisible(0);

View File

@ -44,12 +44,12 @@ public class FactorCompletion extends SideKickCompletion
private FactorParsedData data; private FactorParsedData data;
//{{{ FactorCompletion constructor //{{{ FactorCompletion constructor
public FactorCompletion(View view, List items, public FactorCompletion(View view, FactorWord[] items,
String word, FactorParsedData data) String word, FactorParsedData data)
{ {
this.view = view; this.view = view;
textArea = view.getTextArea(); textArea = view.getTextArea();
this.items = items; this.items = Arrays.asList(items);
this.word = word; this.word = word;
this.data = data; this.data = data;
} //}}} } //}}}

View File

@ -200,6 +200,16 @@ public class FactorPlugin extends EditPlugin
evalInWire(word + " " + op); evalInWire(word + " " + op);
} //}}} } //}}}
//{{{ toWordArray() method
public static FactorWord[] toWordArray(Set completions)
{
FactorWord[] w = (FactorWord[])completions.toArray(new FactorWord[
completions.size()]);
Arrays.sort(w,new MiscUtilities.StringICaseCompare());
return w;
} //}}}
//{{{ getCompletions() method //{{{ getCompletions() method
/** /**
* Returns all words in all vocabularies. * Returns all words in all vocabularies.
@ -207,7 +217,7 @@ public class FactorPlugin extends EditPlugin
* @param anywhere If true, matches anywhere in the word name are * @param anywhere If true, matches anywhere in the word name are
* returned; otherwise, only matches from beginning. * returned; otherwise, only matches from beginning.
*/ */
public static List getCompletions(String word, boolean anywhere) public static Set getCompletions(String word, boolean anywhere)
{ {
try try
{ {
@ -225,11 +235,11 @@ public class FactorPlugin extends EditPlugin
* @param anywhere If true, matches anywhere in the word name are * @param anywhere If true, matches anywhere in the word name are
* returned; otherwise, only matches from beginning. * returned; otherwise, only matches from beginning.
*/ */
public static List getCompletions(Cons use, String word, boolean anywhere) public static Set getCompletions(Cons use, String word, boolean anywhere)
{ {
try try
{ {
List completions = new ArrayList(); Set completions = new HashSet();
while(use != null) while(use != null)
{ {
@ -238,10 +248,7 @@ public class FactorPlugin extends EditPlugin
vocab,word,completions,anywhere); vocab,word,completions,anywhere);
use = use.next(); use = use.next();
} }
Collections.sort(completions,
new MiscUtilities.StringICaseCompare());
return completions; return completions;
} }
catch(Exception e) catch(Exception e)

View File

@ -274,10 +274,11 @@ public class FactorSideKickParser extends SideKickParser
if(word.length() == 0) if(word.length() == 0)
return null; return null;
List completions = FactorPlugin.getCompletions( FactorWord[] completions = FactorPlugin.toWordArray(
data.use,word,false); FactorPlugin.getCompletions(
data.use,word,false));
if(completions.size() == 0) if(completions.length == 0)
return null; return null;
else else
{ {

View File

@ -34,7 +34,6 @@ USE: logic
USE: lists USE: lists
USE: math USE: math
USE: namespaces USE: namespaces
USE: prettyprint
USE: stack USE: stack
USE: stdio USE: stdio
USE: strings USE: strings
@ -42,6 +41,7 @@ USE: presentation
USE: unparser USE: unparser
USE: vectors USE: vectors
USE: words USE: words
USE: hashtables
: tab-size : tab-size
#! Change this to suit your tastes. #! Change this to suit your tastes.
@ -82,10 +82,10 @@ DEFER: prettyprint*
] unless ; ] unless ;
: prettyprint-[ ( indent -- indent ) : prettyprint-[ ( indent -- indent )
"[" write <prettyprint ; \ [ prettyprint-word <prettyprint ;
: prettyprint-] ( indent -- indent ) : prettyprint-] ( indent -- indent )
prettyprint> "]" write ; prettyprint> \ ] prettyprint-word ;
: prettyprint-list ( indent list -- indent ) : prettyprint-list ( indent list -- indent )
#! Pretty-print a list, without [ and ]. #! Pretty-print a list, without [ and ].
@ -104,10 +104,10 @@ DEFER: prettyprint*
swap prettyprint-[ swap prettyprint-list prettyprint-] ; swap prettyprint-[ swap prettyprint-list prettyprint-] ;
: prettyprint-{ ( indent -- indent ) : prettyprint-{ ( indent -- indent )
"{" write <prettyprint ; \ { prettyprint-word <prettyprint ;
: prettyprint-} ( indent -- indent ) : prettyprint-} ( indent -- indent )
prettyprint> "}" write ; prettyprint> \ } prettyprint-word ;
: prettyprint-vector ( indent list -- indent ) : prettyprint-vector ( indent list -- indent )
#! Pretty-print a vector, without { and }. #! Pretty-print a vector, without { and }.
@ -115,11 +115,24 @@ DEFER: prettyprint*
: prettyprint-{} ( indent vector -- indent ) : prettyprint-{} ( indent vector -- indent )
dup vector-length 0 = [ dup vector-length 0 = [
drop "{ }" write drop prettyprint-{ prettyprint-}
] [ ] [
swap prettyprint-{ swap prettyprint-vector prettyprint-} swap prettyprint-{ swap prettyprint-vector prettyprint-}
] ifte ; ] ifte ;
: prettyprint-{{ ( indent -- indent )
\ {{ prettyprint-word <prettyprint ;
: prettyprint-}} ( indent -- indent )
prettyprint> \ }} prettyprint-word ;
: prettyprint-{{}} ( indent hashtable -- indent )
hash>alist dup length 0 = [
drop prettyprint-{{ prettyprint-}}
] [
swap prettyprint-{{ swap prettyprint-list prettyprint-}}
] ifte ;
: trim-newline ( str -- str ) : trim-newline ( str -- str )
dup ends-with-newline? dup [ nip ] [ drop ] ifte ; dup ends-with-newline? dup [ nip ] [ drop ] ifte ;
@ -168,11 +181,12 @@ DEFER: prettyprint*
unparse write unparse write
] [ ] [
[ [
[ f = ] [ prettyprint-object ] [ f = ] [ prettyprint-object ]
[ cons? ] [ prettyprint-[] ] [ cons? ] [ prettyprint-[] ]
[ vector? ] [ prettyprint-{} ] [ hashtable? ] [ prettyprint-{{}} ]
[ word? ] [ prettyprint-word ] [ vector? ] [ prettyprint-{} ]
[ drop t ] [ prettyprint-object ] [ word? ] [ prettyprint-word ]
[ drop t ] [ prettyprint-object ]
] cond ] cond
] ifte ; ] ifte ;

View File

@ -4,6 +4,7 @@ USE: kernel
USE: namespaces USE: namespaces
USE: stack USE: stack
USE: test USE: test
USE: lists
[ f ] [ [ ] [ ] catch ] unit-test [ f ] [ [ ] [ ] catch ] unit-test
@ -14,3 +15,6 @@ USE: test
global [ "error" get ] bind global [ "error" get ] bind
"Hello" = "Hello" =
] unit-test ] unit-test
[ ] [ [ ] print-error ] unit-test
[ ] [ [ 2 car ] print-error ] unit-test

View File

@ -76,4 +76,4 @@ USE: unparser
: print-error ( quot -- ) : print-error ( quot -- )
#! Execute a quotation, and if it throws an error, print it #! Execute a quotation, and if it throws an error, print it
#! and return to the caller. #! and return to the caller.
[ [ default-error-handler drop ] when* ] catch ; [ [ default-error-handler ] when* ] catch ;