|
From: David C. <dc...@us...> - 2005-07-30 21:35:04
|
Update of /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/rubyeditor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19680/src/org/rubypeople/rdt/internal/ui/rubyeditor Modified Files: RubySourceViewer.java Added Files: TabExpander.java Log Message: Fixed space-for-tab substitution to work the way most editors do. --- NEW FILE: TabExpander.java --- package org.rubypeople.rdt.internal.ui.rubyeditor; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.DocumentCommand; import org.eclipse.jface.text.IDocument; public class TabExpander { private final int spacesPerTab; private String maxTabString; public TabExpander(int spacesPerTab) { this.spacesPerTab = spacesPerTab; StringBuffer buffer = new StringBuffer(); for (int i=0; i<spacesPerTab; i++) buffer.append(' '); maxTabString = buffer.toString(); } public void expandTab(DocumentCommand command, IDocument document) { try { int offsetFromFileStart = command.offset; int lineNumber = document.getLineOfOffset(offsetFromFileStart); int offsetFromLineStart = offsetFromFileStart - document.getLineOffset(lineNumber); if (spacesPerTab > 0) { int count = spacesPerTab - offsetFromLineStart % spacesPerTab; command.text = maxTabString.substring(0, count); } else { command.text = " "; } } catch (BadLocationException e) { e.printStackTrace(); } } public String getFullIndent() { return maxTabString; } } Index: RubySourceViewer.java =================================================================== RCS file: /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/rubyeditor/RubySourceViewer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RubySourceViewer.java 2 Mar 2005 00:54:29 -0000 1.2 --- RubySourceViewer.java 30 Jul 2005 21:34:55 -0000 1.3 *************** *** 9,13 **** --- 9,15 ---- import java.util.Arrays; + import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.DocumentCommand; + import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.source.IOverviewRuler; import org.eclipse.jface.text.source.IVerticalRuler; *************** *** 20,26 **** public class RubySourceViewer extends ProjectionViewer { - private String tabReplaceString; private boolean isTabReplacing = false; private boolean fIgnoreTextConverters = false; public RubySourceViewer(Composite composite, IVerticalRuler verticalRuler, --- 22,28 ---- public class RubySourceViewer extends ProjectionViewer { private boolean isTabReplacing = false; private boolean fIgnoreTextConverters = false; + private TabExpander tabExpander; public RubySourceViewer(Composite composite, IVerticalRuler verticalRuler, *************** *** 48,52 **** super.customizeDocumentCommand(command); if (!fIgnoreTextConverters) { ! convertTabs(command); } fIgnoreTextConverters = false; --- 50,54 ---- super.customizeDocumentCommand(command); if (!fIgnoreTextConverters) { ! convertTabs(command, getDocument()); } fIgnoreTextConverters = false; *************** *** 59,73 **** int length = RubyPlugin.getDefault().getPreferenceStore().getInt( PreferenceConstants.FORMAT_INDENTATION); ! char[] spaces = new char[length]; ! Arrays.fill(spaces, ' '); ! tabReplaceString = new String(spaces); } } ! protected void convertTabs(DocumentCommand command) { ! if (!isTabReplacing) { return; } ! if (command.text.equals("\t")) { ! command.text = this.tabReplaceString; ! } } --- 61,74 ---- int length = RubyPlugin.getDefault().getPreferenceStore().getInt( PreferenceConstants.FORMAT_INDENTATION); ! tabExpander = new TabExpander(length); } } ! protected void convertTabs(DocumentCommand command, IDocument document) { ! if (!isTabReplacing) ! return; ! ! if (command.text.equals("\t")) ! tabExpander.expandTab(command, document); } *************** *** 75,86 **** return isTabReplacing; } ! ! /** ! * ! * @return Returns the replacement string for tab if isTabReplacing() ! */ ! public String getTabReplaceString() { ! return tabReplaceString; ! } ! } --- 76,82 ---- return isTabReplacing; } ! ! public String getIndentString() { ! return tabExpander.getFullIndent(); ! } } |