From: <sh...@us...> - 2009-05-07 20:47:59
|
Revision: 15215 http://jedit.svn.sourceforge.net/jedit/?rev=15215&view=rev Author: shlomy Date: 2009-05-07 20:47:56 +0000 (Thu, 07 May 2009) Log Message: ----------- 1. Replaced the usage of the GNU regexp package with the native Java regexp package. 2. Implemented temporary folds. A new action allows you to select an arbitrary range of lines in the buffer and create a fold on these lines, and another action allows you to remove the temporary fold at the caret location. Modified Paths: -------------- plugins/ConfigurableFoldHandler/branches/manualFolds/actions.xml plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/BufferFoldStringsDialog.java plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandler.java plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerOptionsPane.java plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerPlugin.java plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/FoldStringsException.java plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/GNURegexCounter.java plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler.props Added Paths: ----------- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/TemporaryFolds.java Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/actions.xml =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/actions.xml 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/actions.xml 2009-05-07 20:47:56 UTC (rev 15215) @@ -7,4 +7,14 @@ new BufferFoldStringsDialog(view).show(); </CODE> </ACTION> + <ACTION NAME="configurablefoldhandler.create-temp-fold"> + <CODE> + configurablefoldhandler.ConfigurableFoldHandlerPlugin.createTempFold(textArea); + </CODE> + </ACTION> + <ACTION NAME="configurablefoldhandler.remove-temp-fold"> + <CODE> + configurablefoldhandler.ConfigurableFoldHandlerPlugin.removeTempFold(textArea); + </CODE> + </ACTION> </ACTIONS> Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/BufferFoldStringsDialog.java =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/BufferFoldStringsDialog.java 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/BufferFoldStringsDialog.java 2009-05-07 20:47:56 UTC (rev 15215) @@ -23,31 +23,30 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -import javax.swing.JDialog; -import javax.swing.JFrame; -import javax.swing.JPanel; +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + import javax.swing.JButton; -import javax.swing.JLabel; import javax.swing.JCheckBox; -import javax.swing.JTextField; +import javax.swing.JLabel; import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; import javax.swing.border.EmptyBorder; -import java.awt.Insets; -import java.awt.Dimension; -import java.awt.FlowLayout; -import java.awt.BorderLayout; -import java.awt.GridBagLayout; -import java.awt.GridBagConstraints; -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; -import org.gjt.sp.jedit.jEdit; import org.gjt.sp.jedit.View; +import org.gjt.sp.jedit.jEdit; import org.gjt.sp.jedit.gui.EnhancedDialog; /** * the dialog for specifying buffer fold strings */ +@SuppressWarnings("serial") public class BufferFoldStringsDialog extends EnhancedDialog { private JTextField startField; Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandler.java =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandler.java 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandler.java 2009-05-07 20:47:56 UTC (rev 15215) @@ -14,7 +14,7 @@ */ import javax.swing.text.Segment; -import org.gjt.sp.jedit.Buffer; + import org.gjt.sp.jedit.buffer.FoldHandler; import org.gjt.sp.jedit.buffer.JEditBuffer; /** @@ -57,8 +57,18 @@ FoldCounter counter = ConfigurableFoldHandlerPlugin.getInstance() .getCounter(buffer); + TemporaryFolds tf = (TemporaryFolds) buffer.getProperty("tempFolds"); + int tempFoldLevel = 0; + if (tf != null) + { + boolean current = tf.isFold(lineIndex); + boolean prev = tf.isFold(lineIndex - 1); + if (current != prev) + tempFoldLevel = current ? 1 : -1; + } + if(counter == null) - return 0; + return Math.max(0, tempFoldLevel); buffer.getLineText(lineIndex - 1, seg); counter.count(seg); @@ -69,7 +79,8 @@ counter.count(seg); int c2 = counter.getLeadingCloses(); - int foldLevel = buffer.getFoldLevel(lineIndex - 1) + folds + c1 - c2; + int foldLevel = buffer.getFoldLevel(lineIndex - 1) + folds + c1 - c2 + + tempFoldLevel; return Math.max(0, foldLevel); } } Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerOptionsPane.java =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerOptionsPane.java 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerOptionsPane.java 2009-05-07 20:47:56 UTC (rev 15215) @@ -39,6 +39,7 @@ * @author C.J.Kent * @created 15 May 2002 */ +@SuppressWarnings("serial") public class ConfigurableFoldHandlerOptionsPane extends AbstractOptionPane { private JTextField defStartField; @@ -51,7 +52,8 @@ private JCheckBox modeUseRegex; private ConfigurableFoldHandlerPlugin plugin; private String[] modeNames; - private HashMap modeStrings = new HashMap(); + private HashMap<String, FoldStrings> modeStrings = + new HashMap<String, FoldStrings>(); private String lastModeName; private boolean initialising; @@ -196,13 +198,11 @@ String startProp; String endProp; String regexProp; - String modeFoldStart; - String modeFoldEnd; FoldStrings foldStrings; for(int i = 0; i < modeNames.length; i++) { - foldStrings = (FoldStrings)modeStrings.get(modeNames[i]); + foldStrings = modeStrings.get(modeNames[i]); startProp = "configurablefoldhandler.mode." + modeNames[i] + ".startfold"; @@ -249,7 +249,7 @@ { FoldStrings newStr = new FoldStrings(modeStartField.getText(), modeEndField.getText(),modeUseRegex.isSelected()); - FoldStrings oldStr = (FoldStrings)modeStrings.get(lastModeName); + FoldStrings oldStr = modeStrings.get(lastModeName); if (oldStr == null || !newStr.equals(oldStr)) { @@ -288,7 +288,7 @@ private void checkMode() { String modeName = modeNames[modeBox.getSelectedIndex()]; - FoldStrings foldStrings = (FoldStrings)modeStrings.get(modeName); + FoldStrings foldStrings = modeStrings.get(modeName); if(foldStrings != null) { Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerPlugin.java =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerPlugin.java 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/ConfigurableFoldHandlerPlugin.java 2009-05-07 20:47:56 UTC (rev 15215) @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.Iterator; + import org.gjt.sp.jedit.jEdit; import org.gjt.sp.jedit.Mode; import org.gjt.sp.jedit.Buffer; @@ -33,6 +34,8 @@ import org.gjt.sp.jedit.EBPlugin; import org.gjt.sp.jedit.msg.PropertiesChanged; import org.gjt.sp.jedit.msg.EditPaneUpdate; +import org.gjt.sp.jedit.textarea.JEditTextArea; +import org.gjt.sp.jedit.textarea.Selection; /** * plugin to insert a configurable fold handler into jEdit @@ -43,14 +46,17 @@ new FoldStrings("{", "}"); private String[] modeNames; - private HashMap defaultModeStringsMap = new HashMap(); + private HashMap<String, FoldStrings> defaultModeStringsMap = + new HashMap<String, FoldStrings>(); // default fold strings for modes / buffers that have none specified private FoldStrings defFoldStrings; // store the fold strings for specific buffers and edit modes - private HashMap bufferStrings = new HashMap(); - private HashMap modeStrings = new HashMap(); + private HashMap<JEditBuffer, FoldStrings> bufferStrings = + new HashMap<JEditBuffer, FoldStrings>(); + private HashMap<String, FoldStrings> modeStrings = + new HashMap<String, FoldStrings>(); private static ConfigurableFoldHandlerPlugin instance; @@ -83,7 +89,8 @@ Buffer[] buffers = jEdit.getBuffers(); int i; -loop: for(Iterator iter = bufferStrings.keySet().iterator(); iter.hasNext(); ) +loop: for(Iterator<JEditBuffer> iter = bufferStrings.keySet().iterator(); + iter.hasNext(); ) { Buffer curBuffer = (Buffer)iter.next(); @@ -202,13 +209,10 @@ */ public FoldCounter getCounter(JEditBuffer buffer) { - FoldStrings foldStrings = (FoldStrings)bufferStrings.get(buffer); + FoldStrings foldStrings = bufferStrings.get(buffer); if(foldStrings == null) - { - foldStrings = (FoldStrings)modeStrings.get( - buffer.getStringProperty("mode")); - } + foldStrings = modeStrings.get(buffer.getStringProperty("mode")); if(foldStrings == null) foldStrings = defFoldStrings; @@ -252,7 +256,7 @@ */ public FoldStrings getBufferFoldStrings(Buffer buffer) { - return (FoldStrings)bufferStrings.get(buffer); + return bufferStrings.get(buffer); } /** @@ -267,7 +271,7 @@ */ public FoldStrings getModeFoldStrings(String modeName) { - return (FoldStrings)modeStrings.get(modeName); + return modeStrings.get(modeName); } /** @@ -277,7 +281,7 @@ */ public FoldStrings getDefaultModeFoldStrings(String modeName) { - return (FoldStrings)defaultModeStringsMap.get(modeName); + return defaultModeStringsMap.get(modeName); } /** @@ -293,4 +297,41 @@ { return defFoldStrings; } + + /** + * Creates a temporary fold for the selected text. + */ + static public void createTempFold(JEditTextArea ta) + { + Selection [] sel = ta.getSelection(); + if (sel.length != 1) + return; + JEditBuffer buffer = ta.getBuffer(); + TemporaryFolds tf = (TemporaryFolds) buffer.getProperty("tempFolds"); + if (tf == null) + { + tf = new TemporaryFolds(); + buffer.setProperty("tempFolds", tf); + } + int start = sel[0].getStartLine(); + int end = sel[0].getEndLine(); + tf.add(start, end); + buffer.invalidateCachedFoldLevels(); + } + /** + * Removes the temporary fold at the caret position. + */ + static public void removeTempFold(JEditTextArea ta) + { + JEditBuffer buffer = ta.getBuffer(); + TemporaryFolds tf = (TemporaryFolds) buffer.getProperty("tempFolds"); + if (tf == null) + return; + if (tf.remove(ta.getCaretLine())) + { + buffer.invalidateCachedFoldLevels(); + if (tf.isEmpty()) + buffer.setProperty("tempFolds", null); + } + } } Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/FoldStringsException.java =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/FoldStringsException.java 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/FoldStringsException.java 2009-05-07 20:47:56 UTC (rev 15215) @@ -23,10 +23,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -import java.lang.reflect.Method; -import org.gjt.sp.util.Log; - +@SuppressWarnings("serial") public class FoldStringsException extends Exception { public FoldStringsException(Throwable t) Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/GNURegexCounter.java =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/GNURegexCounter.java 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/GNURegexCounter.java 2009-05-07 20:47:56 UTC (rev 15215) @@ -23,11 +23,13 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -import java.io.InputStream; +import java.util.Vector; +import java.util.regex.MatchResult; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + import javax.swing.text.Segment; -import gnu.regexp.RE; -import gnu.regexp.REMatch; -import gnu.regexp.REException; /** * {@link RegexCounter} implementation that uses the <code>gnu.regexp</code> @@ -35,8 +37,8 @@ */ public class GNURegexCounter implements FoldCounter { - private RE startRE; - private RE endRE; + private Pattern startRE; + private Pattern endRE; private int starts; private int leadingCloses; @@ -45,10 +47,10 @@ { try { - startRE = new RE(startRegex); - endRE = new RE(endRegex); + startRE = Pattern.compile(startRegex); + endRE = Pattern.compile(endRegex); } - catch(REException ex) + catch(PatternSyntaxException ex) { throw new IllegalArgumentException(ex.getMessage()); } @@ -59,29 +61,29 @@ */ public void count(Segment seg) { - SegmentInputStream segmentStream = new SegmentInputStream(seg); - REMatch[] startMatches; - REMatch[] endMatches; + Matcher m = startRE.matcher(seg); + Vector<MatchResult> startMatches = new Vector<MatchResult>(); + while (m.find()) + startMatches.add(m.toMatchResult()); + int foldStarts = startMatches.size(); - startMatches = startRE.getAllMatches(segmentStream); - int foldStarts = startMatches.length; + m = endRE.matcher(seg); + Vector<MatchResult> endMatches = new Vector<MatchResult>(); + while (m.find()) + endMatches.add(m.toMatchResult()); + int foldEnds = endMatches.size(); - segmentStream.restartStream(); - - endMatches = endRE.getAllMatches(segmentStream); - int foldEnds = endMatches.length; - starts = foldStarts - foldEnds; - if(startMatches.length == 0 || endMatches.length == 0) + if(foldStarts == 0 || foldEnds == 0) leadingCloses = 0; else { - int startIndex = startMatches[0].getStartIndex(); + int startIndex = startMatches.firstElement().start(); int i; - for(i = 0; i < endMatches.length; i++) - if(endMatches[i].getStartIndex() > startIndex) + for(i = 0; i < foldEnds; i++) + if(endMatches.get(i).start() > startIndex) break; leadingCloses = i; @@ -100,79 +102,4 @@ */ public int getLeadingCloses() { return leadingCloses; } - /** - * <code>InputStream</code> that reads characters from a - * <code>Segment</code>. - */ - private static class SegmentInputStream extends InputStream - { - private Segment seg; - private int index; - private int end; - - SegmentInputStream(Segment seg) - { - this.seg = seg; - index = seg.offset; - end = seg.offset + seg.count; - } - - public int read() - { - if(index == end) - return -1; - else - return seg.array[index++]; - } - - public int read(byte[] b) - { - if(index == end) - return -1; - - int numBytes = Math.min(b.length, available()); - /* System.arraycopy(seg.array, index, b, 0, numBytes); */ - for(int i = 0; i < numBytes; i++) - { - b[i] = (byte) seg.array[index + i]; - } - index += numBytes; - return numBytes; - } - - public int read(byte[] b, int offset, int count) - { - if(index == end) - return -1; - - int numBytes = Math.min(count, available()); - /* System.arraycopy(seg.array, index, b, offset, numBytes); */ - for(int i = 0; i < numBytes; i++) - { - b[offset + i] = (byte) seg.array[index + i]; - } - index += numBytes; - return numBytes; - } - - public int available() - { - return end - index; - } - - public void skip(int n) - { - index += Math.min(n, available()); - } - - public void restartStream() - { - index = seg.offset; - } - - public boolean markSupported() { return false; } - - public void mark() { throw new UnsupportedOperationException(); } - public void reset() { throw new UnsupportedOperationException(); } - } } Added: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/TemporaryFolds.java =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/TemporaryFolds.java (rev 0) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler/TemporaryFolds.java 2009-05-07 20:47:56 UTC (rev 15215) @@ -0,0 +1,52 @@ +package configurablefoldhandler; + +import java.util.Vector; + +public class TemporaryFolds { + private Vector<Integer> start = new Vector<Integer>(); + private Vector<Integer> end = new Vector<Integer>(); + + public TemporaryFolds() + { + } + public void add(int start, int end) + { + this.start.add(start); + this.end.add(end); + } + public boolean remove(int line) + { + int i = getIndex(line, true); + if (i < 0) + return false; + start.remove(i); + end.remove(i); + return true; + + } + private int getIndex(int line, boolean includeFirst) + { + for (int i = 0; i < start.size(); i++) + { + int first = start.get(i); + if (includeFirst) + first--; + int last = end.get(i); + if (first < line && last > line) + return i; + } + return -1; + } + private int getIndex(int line) + { + return getIndex(line, false); + } + public boolean isFold(int line) + { + return getIndex(line) >= 0; + } + public boolean isEmpty() + { + return (start.size() == 0); + } +} Modified: plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler.props =================================================================== --- plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler.props 2009-05-07 20:37:32 UTC (rev 15214) +++ plugins/ConfigurableFoldHandler/branches/manualFolds/configurablefoldhandler.props 2009-05-07 20:47:56 UTC (rev 15215) @@ -8,8 +8,13 @@ plugin.configurablefoldhandler.ConfigurableFoldHandlerPlugin.description=The ConfigurableFoldHandler plugin adds a new fold handler to jEdit that allows the user to specify the strings that define the start and end of a fold. # menu -plugin.configurablefoldhandler.ConfigurableFoldHandlerPlugin.menu=configurablefoldhandler.buffer-fold-strings +plugin.configurablefoldhandler.ConfigurableFoldHandlerPlugin.menu=\ + configurablefoldhandler.buffer-fold-strings \ + configurablefoldhandler.create-temp-fold \ + configurablefoldhandler.remove-temp-fold configurablefoldhandler.buffer-fold-strings.label=Buffer fold strings +configurablefoldhandler.create-temp-fold.label=Create temporary fold for selected text +configurablefoldhandler.remove-temp-fold.label=Remove temporary fold at caret position # options pane plugin.configurablefoldhandler.ConfigurableFoldHandlerPlugin.option-pane=configurablefoldhandler This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |