Buffer processor for generating automatic type unit tests
parent
d2e68b7f9e
commit
fd64bc4ccc
|
@ -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());
|
||||||
|
} //}}}
|
||||||
|
}
|
|
@ -193,6 +193,16 @@ public class FactorPlugin extends EditPlugin
|
||||||
"sidekick.SideKickParser","factor");
|
"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
|
//{{{ evalInListener() method
|
||||||
public static void evalInListener(View view, String cmd)
|
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)
|
public static FactorWord lookupWord(View view, String word)
|
||||||
{
|
{
|
||||||
SideKickParsedData data = SideKickParsedData.getParsedData(view);
|
FactorParsedData fdata = getParsedData(view);
|
||||||
if(data instanceof FactorParsedData)
|
if(fdata == null)
|
||||||
{
|
|
||||||
FactorParsedData fdata = (FactorParsedData)data;
|
|
||||||
return getExternalInstance().searchVocabulary(fdata.use,word);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return null;
|
return null;
|
||||||
|
else
|
||||||
|
return getExternalInstance().searchVocabulary(fdata.use,word);
|
||||||
} //}}}
|
} //}}}
|
||||||
|
|
||||||
//{{{ factorWord() method
|
//{{{ factorWord() method
|
||||||
|
@ -230,18 +237,14 @@ public class FactorPlugin extends EditPlugin
|
||||||
*/
|
*/
|
||||||
public static String factorWord(View view, String word)
|
public static String factorWord(View view, String word)
|
||||||
{
|
{
|
||||||
SideKickParsedData data = SideKickParsedData
|
FactorParsedData fdata = getParsedData(view);
|
||||||
.getParsedData(view);
|
if(fdata == null)
|
||||||
if(data instanceof FactorParsedData)
|
return null;
|
||||||
{
|
|
||||||
FactorParsedData fdata = (FactorParsedData)data;
|
|
||||||
return "\""
|
return "\""
|
||||||
+ FactorReader.charsToEscapes(word)
|
+ FactorReader.charsToEscapes(word)
|
||||||
+ "\" " + FactorReader.unparseObject(fdata.use)
|
+ "\" " + FactorReader.unparseObject(fdata.use)
|
||||||
+ " search";
|
+ " search";
|
||||||
}
|
|
||||||
else
|
|
||||||
return null;
|
|
||||||
} //}}}
|
} //}}}
|
||||||
|
|
||||||
//{{{ factorWord() method
|
//{{{ factorWord() method
|
||||||
|
@ -403,16 +406,14 @@ public class FactorPlugin extends EditPlugin
|
||||||
//{{{ isUsed() method
|
//{{{ isUsed() method
|
||||||
public static boolean isUsed(View view, String vocab)
|
public static boolean isUsed(View view, String vocab)
|
||||||
{
|
{
|
||||||
SideKickParsedData data = SideKickParsedData
|
FactorParsedData fdata = getParsedData(view);
|
||||||
.getParsedData(view);
|
if(fdata == null)
|
||||||
if(data instanceof FactorParsedData)
|
return false;
|
||||||
|
else
|
||||||
{
|
{
|
||||||
FactorParsedData fdata = (FactorParsedData)data;
|
|
||||||
Cons use = fdata.use;
|
Cons use = fdata.use;
|
||||||
return Cons.contains(use,vocab);
|
return Cons.contains(use,vocab);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
return false;
|
|
||||||
} //}}}
|
} //}}}
|
||||||
|
|
||||||
//{{{ findAllWordsNamed() method
|
//{{{ findAllWordsNamed() method
|
||||||
|
@ -514,9 +515,8 @@ public class FactorPlugin extends EditPlugin
|
||||||
if(selection == null)
|
if(selection == null)
|
||||||
selection = "";
|
selection = "";
|
||||||
|
|
||||||
SideKickParsedData data = SideKickParsedData
|
FactorParsedData data = getParsedData(view);
|
||||||
.getParsedData(view);
|
if(data == null)
|
||||||
if(!(data instanceof FactorParsedData))
|
|
||||||
{
|
{
|
||||||
view.getToolkit().beep();
|
view.getToolkit().beep();
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -245,11 +245,10 @@ public class FactorSideKickParser extends SideKickParser
|
||||||
*/
|
*/
|
||||||
public SideKickCompletion complete(EditPane editPane, int caret)
|
public SideKickCompletion complete(EditPane editPane, int caret)
|
||||||
{
|
{
|
||||||
SideKickParsedData _data = SideKickParsedData
|
FactorParsedData data = FactorPlugin.getParsedData(
|
||||||
.getParsedData(editPane.getView());
|
editPane.getView());
|
||||||
if(!(_data instanceof FactorParsedData))
|
if(data == null)
|
||||||
return null;
|
return null;
|
||||||
FactorParsedData data = (FactorParsedData)_data;
|
|
||||||
|
|
||||||
Buffer buffer = editPane.getBuffer();
|
Buffer buffer = editPane.getBuffer();
|
||||||
|
|
||||||
|
|
|
@ -134,10 +134,10 @@ public class WordPreview implements ActionListener, CaretListener
|
||||||
if(SideKickPlugin.isParsingBuffer(view.getBuffer()))
|
if(SideKickPlugin.isParsingBuffer(view.getBuffer()))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
SideKickParsedData data = SideKickParsedData.getParsedData(view);
|
FactorParsedData data = FactorPlugin.getParsedData(view);
|
||||||
if(data instanceof FactorParsedData)
|
if(data != null)
|
||||||
{
|
{
|
||||||
FactorWord w = getWordAtCaret((FactorParsedData)data);
|
FactorWord w = getWordAtCaret(data);
|
||||||
if(w != null)
|
if(w != null)
|
||||||
{
|
{
|
||||||
view.getStatus().setMessageAndClear(
|
view.getStatus().setMessageAndClear(
|
||||||
|
|
Loading…
Reference in New Issue