diff --git a/factor/jedit/FactorBufferProcessor.java b/factor/jedit/FactorBufferProcessor.java new file mode 100644 index 0000000000..a7ea200653 --- /dev/null +++ b/factor/jedit/FactorBufferProcessor.java @@ -0,0 +1,93 @@ +/* :folding=explicit:collapseFolds=1: */ + +/* + * $Id$ + * + * Copyright (C) 2005 Slava Pestov. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package factor.jedit; + +import factor.*; +import java.io.IOException; +import java.util.*; +import org.gjt.sp.jedit.Buffer; + +/** + * A class used to compile all words in a file, or infer stack effects of all + * words in a file, etc. + */ +public class FactorBufferProcessor +{ + private String code; + private LinkedHashMap results; + + //{{{ FactorBufferProcessor constructor + /** + * @param buffer The buffer + * @param code The snippet of code to apply to each word. The snippet + * should print a string. + */ + public FactorBufferProcessor(Buffer buffer, String code, ExternalFactor factor) + throws IOException + { + results = new LinkedHashMap(); + this.code = code; + + Cons words = (Cons)buffer.getProperty( + FactorSideKickParser.WORDS_PROPERTY); + Cons wordCodeMap = null; + while(words != null) + { + FactorWord word = (FactorWord)words.car; + + StringBuffer expression = new StringBuffer(); + expression.append(FactorPlugin.factorWord(word)); + expression.append(" "); + expression.append(code); + + results.put(word,factor.eval(expression.toString())); + + words = words.next(); + } + } //}}} + + //{{{ insertResults() method + public void insertResults(Buffer buffer, int offset) + { + StringBuffer result = new StringBuffer(); + Iterator iter = results.entrySet().iterator(); + while(iter.hasNext()) + { + Map.Entry entry = (Map.Entry)iter.next(); + result.append("[ "); + result.append(((String)entry.getValue()).trim()); + result.append(" ] [ \\ "); + result.append(FactorReader.unparseObject(entry.getKey())); + result.append(code); + result.append(" ] unit-test\n"); + } + buffer.insert(offset,result.toString()); + } //}}} +} diff --git a/factor/jedit/FactorPlugin.java b/factor/jedit/FactorPlugin.java index b1b1c583ec..b8c7a4e3c0 100644 --- a/factor/jedit/FactorPlugin.java +++ b/factor/jedit/FactorPlugin.java @@ -193,6 +193,16 @@ public class FactorPlugin extends EditPlugin "sidekick.SideKickParser","factor"); } //}}} + //{{{ getParsedData() method + public static FactorParsedData getParsedData(View view) + { + SideKickParsedData data = SideKickParsedData.getParsedData(view); + if(data instanceof FactorParsedData) + return (FactorParsedData)data; + else + return null; + } //}}} + //{{{ evalInListener() method public static void evalInListener(View view, String cmd) { @@ -214,14 +224,11 @@ public class FactorPlugin extends EditPlugin */ public static FactorWord lookupWord(View view, String word) { - SideKickParsedData data = SideKickParsedData.getParsedData(view); - if(data instanceof FactorParsedData) - { - FactorParsedData fdata = (FactorParsedData)data; - return getExternalInstance().searchVocabulary(fdata.use,word); - } - else + FactorParsedData fdata = getParsedData(view); + if(fdata == null) return null; + else + return getExternalInstance().searchVocabulary(fdata.use,word); } //}}} //{{{ factorWord() method @@ -230,18 +237,14 @@ public class FactorPlugin extends EditPlugin */ public static String factorWord(View view, String word) { - SideKickParsedData data = SideKickParsedData - .getParsedData(view); - if(data instanceof FactorParsedData) - { - FactorParsedData fdata = (FactorParsedData)data; - return "\"" - + FactorReader.charsToEscapes(word) - + "\" " + FactorReader.unparseObject(fdata.use) - + " search"; - } - else + FactorParsedData fdata = getParsedData(view); + if(fdata == null) return null; + + return "\"" + + FactorReader.charsToEscapes(word) + + "\" " + FactorReader.unparseObject(fdata.use) + + " search"; } //}}} //{{{ factorWord() method @@ -403,16 +406,14 @@ public class FactorPlugin extends EditPlugin //{{{ isUsed() method public static boolean isUsed(View view, String vocab) { - SideKickParsedData data = SideKickParsedData - .getParsedData(view); - if(data instanceof FactorParsedData) + FactorParsedData fdata = getParsedData(view); + if(fdata == null) + return false; + else { - FactorParsedData fdata = (FactorParsedData)data; Cons use = fdata.use; return Cons.contains(use,vocab); } - else - return false; } //}}} //{{{ findAllWordsNamed() method @@ -514,9 +515,8 @@ public class FactorPlugin extends EditPlugin if(selection == null) selection = ""; - SideKickParsedData data = SideKickParsedData - .getParsedData(view); - if(!(data instanceof FactorParsedData)) + FactorParsedData data = getParsedData(view); + if(data == null) { view.getToolkit().beep(); return; diff --git a/factor/jedit/FactorSideKickParser.java b/factor/jedit/FactorSideKickParser.java index 6c3649d572..8afd403d1a 100644 --- a/factor/jedit/FactorSideKickParser.java +++ b/factor/jedit/FactorSideKickParser.java @@ -245,11 +245,10 @@ public class FactorSideKickParser extends SideKickParser */ public SideKickCompletion complete(EditPane editPane, int caret) { - SideKickParsedData _data = SideKickParsedData - .getParsedData(editPane.getView()); - if(!(_data instanceof FactorParsedData)) + FactorParsedData data = FactorPlugin.getParsedData( + editPane.getView()); + if(data == null) return null; - FactorParsedData data = (FactorParsedData)_data; Buffer buffer = editPane.getBuffer(); diff --git a/factor/jedit/WordPreview.java b/factor/jedit/WordPreview.java index fcf52aeaf2..6e33284617 100644 --- a/factor/jedit/WordPreview.java +++ b/factor/jedit/WordPreview.java @@ -134,10 +134,10 @@ public class WordPreview implements ActionListener, CaretListener if(SideKickPlugin.isParsingBuffer(view.getBuffer())) return; - SideKickParsedData data = SideKickParsedData.getParsedData(view); - if(data instanceof FactorParsedData) + FactorParsedData data = FactorPlugin.getParsedData(view); + if(data != null) { - FactorWord w = getWordAtCaret((FactorParsedData)data); + FactorWord w = getWordAtCaret(data); if(w != null) { view.getStatus().setMessageAndClear(