Lines 67-76
Link Here
|
67 |
import javax.swing.text.ViewFactory; |
67 |
import javax.swing.text.ViewFactory; |
68 |
import javax.swing.text.Caret; |
68 |
import javax.swing.text.Caret; |
69 |
import javax.swing.text.JTextComponent; |
69 |
import javax.swing.text.JTextComponent; |
70 |
import java.io.CharArrayWriter; |
|
|
71 |
import java.lang.reflect.Method; |
70 |
import java.lang.reflect.Method; |
72 |
import java.util.Set; |
71 |
import java.util.Set; |
73 |
import java.util.Vector; |
|
|
74 |
import java.util.WeakHashMap; |
72 |
import java.util.WeakHashMap; |
75 |
import java.util.logging.Level; |
73 |
import java.util.logging.Level; |
76 |
import java.util.logging.Logger; |
74 |
import java.util.logging.Logger; |
Lines 87-100
Link Here
|
87 |
import org.netbeans.api.editor.mimelookup.MimeLookup; |
85 |
import org.netbeans.api.editor.mimelookup.MimeLookup; |
88 |
import org.netbeans.api.editor.mimelookup.MimePath; |
86 |
import org.netbeans.api.editor.mimelookup.MimePath; |
89 |
import org.netbeans.api.editor.settings.KeyBindingSettings; |
87 |
import org.netbeans.api.editor.settings.KeyBindingSettings; |
|
|
88 |
import org.netbeans.api.editor.settings.SimpleValueNames; |
89 |
import org.netbeans.lib.editor.util.CharSequenceUtilities; |
90 |
import org.netbeans.lib.editor.util.ListenerList; |
90 |
import org.netbeans.lib.editor.util.ListenerList; |
91 |
import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
91 |
import org.netbeans.lib.editor.util.swing.DocumentUtilities; |
|
|
92 |
import org.netbeans.modules.editor.indent.api.Indent; |
93 |
import org.netbeans.modules.editor.indent.api.IndentUtils; |
94 |
import org.netbeans.modules.editor.indent.api.Reformat; |
95 |
import org.netbeans.modules.editor.indent.spi.CodeStylePreferences; |
92 |
import org.netbeans.modules.editor.lib2.EditorPreferencesDefaults; |
96 |
import org.netbeans.modules.editor.lib2.EditorPreferencesDefaults; |
93 |
import org.netbeans.modules.editor.lib2.EditorPreferencesKeys; |
97 |
import org.netbeans.modules.editor.lib2.EditorPreferencesKeys; |
94 |
import org.netbeans.modules.editor.lib.KitsTracker; |
98 |
import org.netbeans.modules.editor.lib.KitsTracker; |
95 |
import org.netbeans.modules.editor.lib.NavigationHistory; |
99 |
import org.netbeans.modules.editor.lib.NavigationHistory; |
96 |
import org.netbeans.modules.editor.lib.SettingsConversions; |
100 |
import org.netbeans.modules.editor.lib.SettingsConversions; |
97 |
import org.netbeans.modules.editor.lib2.highlighting.HighlightingManager; |
101 |
import org.netbeans.modules.editor.lib2.highlighting.HighlightingManager; |
|
|
102 |
import org.netbeans.modules.editor.lib2.typinghooks.DeletedTextInterceptorsManager; |
103 |
import org.netbeans.modules.editor.lib2.typinghooks.TypedBreakInterceptorsManager; |
104 |
import org.netbeans.modules.editor.lib2.typinghooks.TypedTextInterceptorsManager; |
98 |
import org.openide.awt.StatusDisplayer; |
105 |
import org.openide.awt.StatusDisplayer; |
99 |
import org.openide.util.HelpCtx; |
106 |
import org.openide.util.HelpCtx; |
100 |
import org.openide.util.Lookup; |
107 |
import org.openide.util.Lookup; |
Lines 616-629
Link Here
|
616 |
return new SyntaxSupport(doc); |
623 |
return new SyntaxSupport(doc); |
617 |
} |
624 |
} |
618 |
|
625 |
|
619 |
/** |
626 |
// XXX: formatting cleanup |
620 |
* Create the formatter appropriate for this kit |
627 |
// /** |
621 |
* @deprecated Please use Editor Indentation API instead, for details see |
628 |
// * Create the formatter appropriate for this kit |
622 |
* <a href="@org-netbeans-modules-editor-indent@/overview-summary.html">Editor Indentation</a>. |
629 |
// * @deprecated Please use Editor Indentation API instead, for details see |
623 |
*/ |
630 |
// * <a href="@org-netbeans-modules-editor-indent@/overview-summary.html">Editor Indentation</a>. |
624 |
public Formatter createFormatter() { |
631 |
// */ |
625 |
return new Formatter(this.getClass()); |
632 |
// public Formatter createFormatter() { |
626 |
} |
633 |
// return new Formatter(this.getClass()); |
|
|
634 |
// } |
627 |
|
635 |
|
628 |
/** Create text UI */ |
636 |
/** Create text UI */ |
629 |
protected BaseTextUI createTextUI() { |
637 |
protected BaseTextUI createTextUI() { |
Lines 1020-1030
Link Here
|
1020 |
|
1028 |
|
1021 |
|
1029 |
|
1022 |
|
1030 |
|
1023 |
/** Default typed action */ |
1031 |
/** |
|
|
1032 |
* Default typed action |
1033 |
* |
1034 |
* @deprecated Please do not subclass this class. Use Typing Hooks instead, for details see |
1035 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1036 |
*/ |
1024 |
// @EditorActionRegistration(name = defaultKeyTypedAction) |
1037 |
// @EditorActionRegistration(name = defaultKeyTypedAction) |
1025 |
public static class DefaultKeyTypedAction extends LocalBaseAction { |
1038 |
public static class DefaultKeyTypedAction extends LocalBaseAction { |
1026 |
|
1039 |
|
1027 |
static final long serialVersionUID =3069164318144463899L; |
1040 |
static final long serialVersionUID = 3069164318144463899L; |
1028 |
|
1041 |
|
1029 |
public DefaultKeyTypedAction() { |
1042 |
public DefaultKeyTypedAction() { |
1030 |
// Construct with defaultKeyTypedAction name to retain full compatibility for extending actions |
1043 |
// Construct with defaultKeyTypedAction name to retain full compatibility for extending actions |
Lines 1033-1040
Link Here
|
1033 |
LOG.fine("DefaultKeyTypedAction with enhanced logging, see issue #145306"); //NOI18N |
1046 |
LOG.fine("DefaultKeyTypedAction with enhanced logging, see issue #145306"); //NOI18N |
1034 |
} |
1047 |
} |
1035 |
|
1048 |
|
1036 |
private static final boolean isMac = System.getProperty("mrj.version") != null; //NOI18N |
|
|
1037 |
|
1038 |
public void actionPerformed (final ActionEvent evt, final JTextComponent target) { |
1049 |
public void actionPerformed (final ActionEvent evt, final JTextComponent target) { |
1039 |
if ((target != null) && (evt != null)) { |
1050 |
if ((target != null) && (evt != null)) { |
1040 |
|
1051 |
|
Lines 1043-1049
Link Here
|
1043 |
boolean ctrl = ((mod & ActionEvent.CTRL_MASK) != 0); |
1054 |
boolean ctrl = ((mod & ActionEvent.CTRL_MASK) != 0); |
1044 |
// On the mac, norwegian and french keyboards use Alt to do bracket characters. |
1055 |
// On the mac, norwegian and french keyboards use Alt to do bracket characters. |
1045 |
// This replicates Apple's modification DefaultEditorKit.DefaultKeyTypedAction |
1056 |
// This replicates Apple's modification DefaultEditorKit.DefaultKeyTypedAction |
1046 |
boolean alt = isMac ? ((mod & ActionEvent.META_MASK) != 0) : |
1057 |
boolean alt = org.openide.util.Utilities.isMac() ? ((mod & ActionEvent.META_MASK) != 0) : |
1047 |
((mod & ActionEvent.ALT_MASK) != 0); |
1058 |
((mod & ActionEvent.ALT_MASK) != 0); |
1048 |
|
1059 |
|
1049 |
|
1060 |
|
Lines 1057-1123
Link Here
|
1057 |
return; |
1068 |
return; |
1058 |
} |
1069 |
} |
1059 |
|
1070 |
|
1060 |
final Caret caret = target.getCaret(); |
|
|
1061 |
final BaseDocument doc = (BaseDocument)target.getDocument(); |
1062 |
final EditorUI editorUI = Utilities.getEditorUI(target); |
1063 |
// determine if typed char is valid |
1071 |
// determine if typed char is valid |
1064 |
final String cmd = evt.getActionCommand(); |
1072 |
final String cmd = evt.getActionCommand(); |
1065 |
if ((cmd != null) && (cmd.length() == 1)) { |
1073 |
if (cmd != null && cmd.length() == 1 && cmd.charAt(0) >= 0x20 && cmd.charAt(0) != 0x7F) { |
1066 |
// Utilities.clearStatusText(target); |
1074 |
if (LOG.isLoggable(Level.FINE)) { |
|
|
1075 |
LOG.log(Level.FINE, "Processing command char: {0}", Integer.toHexString(cmd.charAt(0))); //NOI18N |
1076 |
} |
1067 |
|
1077 |
|
|
|
1078 |
final BaseDocument doc = (BaseDocument)target.getDocument(); |
1079 |
final int insertionOffset = computeInsertionOffset(target.getCaret()); |
1080 |
final TypedTextInterceptorsManager.Transaction transaction = TypedTextInterceptorsManager.getInstance().openTransaction( |
1081 |
target, insertionOffset, cmd); |
1082 |
|
1068 |
try { |
1083 |
try { |
1069 |
NavigationHistory.getEdits().markWaypoint(target, caret.getDot(), false, true); |
1084 |
if (!transaction.beforeInsertion()) { |
1070 |
} catch (BadLocationException e) { |
1085 |
final Object [] result = new Object [] { Boolean.FALSE, "" }; //NOI18N |
1071 |
LOG.log(Level.WARNING, "Can't add position to the history of edits.", e); //NOI18N |
1086 |
doc.runAtomicAsUser (new Runnable () { |
1072 |
} |
1087 |
public void run () { |
1073 |
|
1088 |
Object [] r = transaction.textTyped(); |
1074 |
doc.runAtomicAsUser (new Runnable () { |
1089 |
String insertionText = r == null ? cmd : (String) r[0]; |
1075 |
public void run () { |
1090 |
int caretPosition = r == null ? -1 : (Integer) r[1]; |
1076 |
DocumentUtilities.setTypingModification(doc, true); |
1091 |
|
1077 |
try { |
|
|
1078 |
char ch = cmd.charAt(0); |
1079 |
if ((ch >= 0x20) && (ch != 0x7F)) { // valid character |
1080 |
LOG.fine("Processing command char: " + Integer.toHexString(ch)); //NOI18N |
1081 |
|
1082 |
editorUI.getWordMatch().clear(); // reset word matching |
1083 |
Boolean overwriteMode = (Boolean)editorUI.getProperty( |
1084 |
EditorUI.OVERWRITE_MODE_PROPERTY); |
1085 |
try { |
1092 |
try { |
1086 |
boolean doInsert = true; // editorUI.getAbbrev().checkAndExpand(ch, evt); |
1093 |
performTextInsertion(target, insertionOffset, insertionText, caretPosition); |
1087 |
if (doInsert) { |
1094 |
result[0] = Boolean.TRUE; |
1088 |
if (Utilities.isSelectionShowing(caret)) { // valid selection |
1095 |
result[1] = insertionText; |
1089 |
boolean ovr = (overwriteMode != null && overwriteMode.booleanValue()); |
1096 |
} catch (BadLocationException ble) { |
1090 |
try { |
1097 |
LOG.log(Level.FINE, null, ble); |
1091 |
doc.putProperty(DOC_REPLACE_SELECTION_PROPERTY, true); |
|
|
1092 |
replaceSelection(target, caret.getDot(), caret, cmd, ovr); |
1093 |
} finally { |
1094 |
doc.putProperty(DOC_REPLACE_SELECTION_PROPERTY, null); |
1095 |
} |
1096 |
} else { // no selection |
1097 |
int dotPos = caret.getDot(); |
1098 |
if (overwriteMode != null && overwriteMode.booleanValue() |
1099 |
&& dotPos < doc.getLength() && doc.getChars(dotPos, 1)[0] != '\n' |
1100 |
) { // overwrite current char |
1101 |
insertString(doc, dotPos, caret, cmd, true); |
1102 |
} else { // insert mode |
1103 |
insertString(doc, dotPos, caret, cmd, false); |
1104 |
} |
1105 |
} |
1106 |
} |
1107 |
} catch (BadLocationException e) { |
1108 |
LOG.log(Level.FINE, null, e); |
1109 |
target.getToolkit().beep(); |
1098 |
target.getToolkit().beep(); |
1110 |
} |
1099 |
} |
1111 |
} else { |
|
|
1112 |
LOG.fine("Invalid command char: " + Integer.toHexString(ch)); //NOI18N |
1113 |
} |
1100 |
} |
|
|
1101 |
}); |
1102 |
|
1103 |
if (((Boolean)result[0]).booleanValue()) { |
1104 |
transaction.afterInsertion(); |
1114 |
|
1105 |
|
1115 |
checkIndent(target, cmd); |
1106 |
// XXX: this is potentially wrong and we may need to call this with |
1116 |
} finally { |
1107 |
// the original cmd; or maybe only if insertionText == cmd; but maybe |
1117 |
DocumentUtilities.setTypingModification(doc, false); |
1108 |
// it does not matter, because nobody seems to be overwriting this method anyway |
1118 |
} |
1109 |
checkIndent(target, (String)result[1]); |
|
|
1110 |
} // else text insertion failed |
1119 |
} |
1111 |
} |
1120 |
}); |
1112 |
} finally { |
|
|
1113 |
transaction.close(); |
1114 |
} |
1121 |
} else { |
1115 |
} else { |
1122 |
if (LOG.isLoggable(Level.FINE)) { |
1116 |
if (LOG.isLoggable(Level.FINE)) { |
1123 |
StringBuilder sb = new StringBuilder(); |
1117 |
StringBuilder sb = new StringBuilder(); |
Lines 1128-1181
Link Here
|
1128 |
sb.append(" "); |
1122 |
sb.append(" "); |
1129 |
} |
1123 |
} |
1130 |
} |
1124 |
} |
1131 |
LOG.fine("Invalid command: '" + sb + "'"); //NOI18N |
1125 |
LOG.log(Level.FINE, "Invalid command: {0}", sb); //NOI18N |
1132 |
} |
1126 |
} |
1133 |
} |
1127 |
} |
1134 |
} |
1128 |
} |
1135 |
} |
1129 |
} |
1136 |
|
1130 |
|
1137 |
/** |
1131 |
// -------------------------------------------------------------------- |
1138 |
* Hook to insert the given string at the given position into |
1132 |
// SPI |
1139 |
* the given document in insert-mode, no selection, writeable |
1133 |
// -------------------------------------------------------------------- |
1140 |
* document. Designed to be overridden by subclasses that want |
1134 |
|
1141 |
* to intercept inserted characters. |
1135 |
/** |
1142 |
*/ |
1136 |
* Hook to insert the given string at the given position into |
1143 |
protected void insertString(BaseDocument doc, |
1137 |
* the given document in insert-mode, no selection, writeable |
|
|
1138 |
* document. Designed to be overridden by subclasses that want |
1139 |
* to intercept inserted characters. |
1140 |
* |
1141 |
* @deprecated Please use Typing Hooks instead, for details see |
1142 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1143 |
*/ |
1144 |
protected void insertString(BaseDocument doc, |
1144 |
int dotPos, |
1145 |
int dotPos, |
1145 |
Caret caret, |
1146 |
Caret caret, |
1146 |
String str, |
1147 |
String str, |
1147 |
boolean overwrite) |
1148 |
boolean overwrite) throws BadLocationException |
1148 |
throws BadLocationException |
1149 |
{ |
1149 |
{ |
1150 |
if (overwrite) { |
1150 |
if (overwrite) doc.remove(dotPos, 1); |
1151 |
doc.remove(dotPos, 1); |
1151 |
doc.insertString(dotPos, str, null); |
1152 |
} |
1152 |
} |
1153 |
|
|
|
1154 |
doc.insertString(dotPos, str, null); |
1155 |
} |
1153 |
|
1156 |
|
1154 |
/** |
1157 |
/** |
1155 |
* Hook to insert the given string at the given position into |
1158 |
* Hook to insert the given string at the given position into |
1156 |
* the given document in insert-mode with selection visible |
1159 |
* the given document in insert-mode with selection visible |
1157 |
* Designed to be overridden by subclasses that want |
1160 |
* Designed to be overridden by subclasses that want |
1158 |
* to intercept inserted characters. |
1161 |
* to intercept inserted characters. |
1159 |
*/ |
1162 |
* |
1160 |
protected void replaceSelection(JTextComponent target, |
1163 |
* @deprecated Please use Typing Hooks instead, for details see |
1161 |
int dotPos, |
1164 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1162 |
Caret caret, |
1165 |
*/ |
1163 |
String str, |
1166 |
protected void replaceSelection( |
1164 |
boolean overwrite) |
1167 |
JTextComponent target, |
1165 |
throws BadLocationException |
1168 |
int dotPos, |
1166 |
{ |
1169 |
Caret caret, |
1167 |
target.replaceSelection(str); |
1170 |
String str, |
1168 |
} |
1171 |
boolean overwrite) throws BadLocationException |
|
|
1172 |
{ |
1173 |
target.replaceSelection(str); |
1174 |
} |
1169 |
|
1175 |
|
1170 |
|
1176 |
/** |
1171 |
/** Check whether there was any important character typed |
1177 |
* Check whether there was any important character typed |
1172 |
* so that the line should be possibly reformatted. |
1178 |
* so that the line should be possibly reformatted. |
1173 |
*/ |
1179 |
* |
|
|
1180 |
* @deprecated Please use <a href="@org-netbeans-modules-editor-indent@/org/netbeans/modules/editor/indent/spi/AutomatedIndenting.html">AutomatedIndentig</a> |
1181 |
* or Typing Hooks instead, for details see |
1182 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1183 |
*/ |
1174 |
protected void checkIndent(JTextComponent target, String typedText) { |
1184 |
protected void checkIndent(JTextComponent target, String typedText) { |
1175 |
} |
1185 |
} |
1176 |
|
1186 |
|
1177 |
} |
1187 |
// -------------------------------------------------------------------- |
|
|
1188 |
// Private implementation |
1189 |
// -------------------------------------------------------------------- |
1178 |
|
1190 |
|
|
|
1191 |
private void performTextInsertion(JTextComponent target, int insertionOffset, String insertionText, int caretPosition) throws BadLocationException { |
1192 |
final BaseDocument doc = (BaseDocument)target.getDocument(); |
1193 |
|
1194 |
try { |
1195 |
NavigationHistory.getEdits().markWaypoint(target, insertionOffset, false, true); |
1196 |
} catch (BadLocationException e) { |
1197 |
LOG.log(Level.WARNING, "Can't add position to the history of edits.", e); //NOI18N |
1198 |
} |
1199 |
|
1200 |
DocumentUtilities.setTypingModification(doc, true); |
1201 |
try { |
1202 |
EditorUI editorUI = Utilities.getEditorUI(target); |
1203 |
Caret caret = target.getCaret(); |
1204 |
|
1205 |
editorUI.getWordMatch().clear(); // reset word matching |
1206 |
Boolean overwriteMode = (Boolean)editorUI.getProperty(EditorUI.OVERWRITE_MODE_PROPERTY); |
1207 |
boolean ovr = (overwriteMode != null && overwriteMode.booleanValue()); |
1208 |
if (Utilities.isSelectionShowing(caret)) { // valid selection |
1209 |
try { |
1210 |
doc.putProperty(DOC_REPLACE_SELECTION_PROPERTY, true); |
1211 |
replaceSelection(target, insertionOffset, caret, insertionText, ovr); |
1212 |
} finally { |
1213 |
doc.putProperty(DOC_REPLACE_SELECTION_PROPERTY, null); |
1214 |
} |
1215 |
} else { // no selection |
1216 |
if (ovr && insertionOffset < doc.getLength() && doc.getChars(insertionOffset, 1)[0] != '\n') { //NOI18N |
1217 |
// overwrite current char |
1218 |
insertString(doc, insertionOffset, caret, insertionText, true); |
1219 |
} else { // insert mode |
1220 |
insertString(doc, insertionOffset, caret, insertionText, false); |
1221 |
} |
1222 |
} |
1223 |
|
1224 |
if (caretPosition != -1) { |
1225 |
assert caretPosition >= 0 && caretPosition < insertionText.length(); |
1226 |
caret.setDot(insertionOffset + caretPosition); |
1227 |
} |
1228 |
} finally { |
1229 |
DocumentUtilities.setTypingModification(doc, false); |
1230 |
} |
1231 |
} |
1232 |
|
1233 |
private int computeInsertionOffset(Caret caret) { |
1234 |
if (Utilities.isSelectionShowing(caret)) { |
1235 |
return Math.min(caret.getMark(), caret.getDot()); |
1236 |
} else { |
1237 |
return caret.getDot(); |
1238 |
} |
1239 |
} |
1240 |
} // End of DefaultKeyTypedAction class |
1241 |
|
1242 |
/** |
1243 |
* @deprecated Please do not subclass this class. Use Typing Hooks instead, for details see |
1244 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1245 |
*/ |
1179 |
public static class InsertBreakAction extends LocalBaseAction { |
1246 |
public static class InsertBreakAction extends LocalBaseAction { |
1180 |
|
1247 |
|
1181 |
static final long serialVersionUID =7966576342334158659L; |
1248 |
static final long serialVersionUID =7966576342334158659L; |
Lines 1192-1242
Link Here
|
1192 |
} |
1259 |
} |
1193 |
|
1260 |
|
1194 |
final BaseDocument doc = (BaseDocument)target.getDocument(); |
1261 |
final BaseDocument doc = (BaseDocument)target.getDocument(); |
1195 |
final Formatter formatter = doc.getFormatter(); |
1262 |
final int insertionOffset = computeInsertionOffset(target.getCaret()); |
1196 |
formatter.indentLock(); |
1263 |
final TypedBreakInterceptorsManager.Transaction transaction = TypedBreakInterceptorsManager.getInstance().openTransaction( |
|
|
1264 |
target, insertionOffset, insertionOffset); |
1265 |
|
1197 |
try { |
1266 |
try { |
1198 |
doc.runAtomicAsUser (new Runnable () { |
1267 |
if (!transaction.beforeInsertion()) { |
1199 |
public void run () { |
1268 |
final Boolean [] result = new Boolean [] { Boolean.FALSE }; //NOI18N |
1200 |
DocumentUtilities.setTypingModification(doc, true); |
1269 |
final Indent indenter = Indent.get(doc); |
1201 |
try { |
1270 |
indenter.lock(); |
1202 |
target.replaceSelection(""); |
1271 |
try { |
1203 |
Caret caret = target.getCaret(); |
1272 |
doc.runAtomicAsUser (new Runnable () { |
1204 |
Object cookie = beforeBreak(target, doc, caret); |
1273 |
public void run () { |
|
|
1274 |
Object [] r = transaction.textTyped(); |
1275 |
String insertionText = r == null ? "\n" : (String) r[0]; //NOI18N |
1276 |
int breakInsertPosition = r == null ? -1 : (Integer) r[1]; |
1277 |
int caretPosition = r == null ? -1 : (Integer) r[2]; |
1278 |
int [] reindentBlocks = r == null ? null : (int []) r[3]; |
1205 |
|
1279 |
|
1206 |
int dotPos = caret.getDot(); |
1280 |
try { |
1207 |
int newDotPos = formatter.indentNewLine(doc, dotPos); |
1281 |
performLineBreakInsertion(target, insertionOffset, insertionText, breakInsertPosition, caretPosition, reindentBlocks, indenter); |
1208 |
caret.setDot(newDotPos); |
1282 |
result[0] = Boolean.TRUE; |
1209 |
|
1283 |
} catch (BadLocationException ble) { |
1210 |
afterBreak(target, doc, caret, cookie); |
1284 |
LOG.log(Level.FINE, null, ble); |
1211 |
} finally { |
1285 |
target.getToolkit().beep(); |
1212 |
DocumentUtilities.setTypingModification(doc, false); |
1286 |
} |
1213 |
} |
1287 |
} |
|
|
1288 |
}); |
1289 |
} finally { |
1290 |
indenter.unlock(); |
1214 |
} |
1291 |
} |
1215 |
}); |
1292 |
|
|
|
1293 |
if (result[0].booleanValue()) { |
1294 |
transaction.afterInsertion(); |
1295 |
} // else line-break insertion failed |
1296 |
|
1297 |
} |
1216 |
} finally { |
1298 |
} finally { |
1217 |
formatter.indentUnlock(); |
1299 |
transaction.close(); |
1218 |
} |
1300 |
} |
|
|
1301 |
|
1219 |
} |
1302 |
} |
1220 |
} |
1303 |
} |
1221 |
|
1304 |
|
1222 |
/** |
1305 |
// -------------------------------------------------------------------- |
1223 |
* Hook called before any changes to the document. The value |
1306 |
// SPI |
1224 |
* returned is passed intact to the other hook. |
1307 |
// -------------------------------------------------------------------- |
1225 |
*/ |
|
|
1226 |
protected Object beforeBreak(JTextComponent target, BaseDocument doc, Caret caret) { |
1227 |
return null; |
1228 |
} |
1229 |
|
1308 |
|
1230 |
/** |
1309 |
/** |
1231 |
* Hook called after the enter was inserted and cursor |
1310 |
* Hook called before any changes to the document. The value |
1232 |
* repositioned. *data* is the object returned previously by |
1311 |
* returned is passed intact to the other hook. |
1233 |
* *beforeBreak* hook. By default null. |
1312 |
* |
1234 |
*/ |
1313 |
* @deprecated Please use Typing Hooks instead, for details see |
1235 |
protected void afterBreak(JTextComponent target, BaseDocument doc, Caret caret, Object data) { |
1314 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1236 |
} |
1315 |
*/ |
1237 |
} |
1316 |
protected Object beforeBreak(JTextComponent target, BaseDocument doc, Caret caret) { |
|
|
1317 |
return null; |
1318 |
} |
1238 |
|
1319 |
|
1239 |
@EditorActionRegistration(name = splitLineAction) |
1320 |
/** |
|
|
1321 |
* Hook called after the enter was inserted and cursor |
1322 |
* repositioned. |
1323 |
* |
1324 |
* @param data the object returned from previously called |
1325 |
* {@link #beforeBreak(javax.swing.text.JTextComponent, org.netbeans.editor.BaseDocument, javax.swing.text.Caret)} hook. |
1326 |
* By default <code>null</code>. |
1327 |
* |
1328 |
* @deprecated Please use Typing Hooks instead, for details see |
1329 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1330 |
*/ |
1331 |
protected void afterBreak(JTextComponent target, BaseDocument doc, Caret caret, Object data) { |
1332 |
} |
1333 |
|
1334 |
// -------------------------------------------------------------------- |
1335 |
// Private implementation |
1336 |
// -------------------------------------------------------------------- |
1337 |
|
1338 |
private void performLineBreakInsertion( |
1339 |
JTextComponent target, |
1340 |
int insertionOffset, |
1341 |
String insertionText, |
1342 |
int breakInsertPosition, |
1343 |
int caretPosition, |
1344 |
int [] reindentBlocks, |
1345 |
Indent indenter) throws BadLocationException |
1346 |
{ |
1347 |
BaseDocument doc = (BaseDocument) target.getDocument(); |
1348 |
DocumentUtilities.setTypingModification(doc, true); |
1349 |
try { |
1350 |
target.replaceSelection(""); // NOI18N |
1351 |
Caret caret = target.getCaret(); |
1352 |
Object cookie = beforeBreak(target, doc, caret); |
1353 |
|
1354 |
// insert new line, caret moves to the new line |
1355 |
int dotPos = caret.getDot(); |
1356 |
assert dotPos == insertionOffset : "dotPos=" + dotPos + " != " + "insertionOffset=" + insertionOffset; //NOI18N |
1357 |
// doc.insertString(dotPos, "\n", null); //NOI18N |
1358 |
// dotPos++; |
1359 |
doc.insertString(dotPos, insertionText, null); |
1360 |
dotPos += caretPosition != -1 ? caretPosition : |
1361 |
breakInsertPosition != -1 ? breakInsertPosition + 1 : |
1362 |
insertionText.indexOf('\n') + 1; //NOI18N |
1363 |
|
1364 |
// reindent the new line |
1365 |
Position newDotPos = doc.createPosition(dotPos); |
1366 |
if (reindentBlocks != null && reindentBlocks.length > 0) { |
1367 |
for(int i = 0; i < reindentBlocks.length / 2; i++) { |
1368 |
int startOffset = insertionOffset + reindentBlocks[2 * i]; |
1369 |
int endOffset = insertionOffset + reindentBlocks[2 * i + 1]; |
1370 |
indenter.reindent(startOffset, endOffset); |
1371 |
} |
1372 |
} else { |
1373 |
indenter.reindent(dotPos); |
1374 |
} |
1375 |
|
1376 |
// adjust the caret |
1377 |
caret.setDot(newDotPos.getOffset()); |
1378 |
|
1379 |
afterBreak(target, doc, caret, cookie); |
1380 |
} finally { |
1381 |
DocumentUtilities.setTypingModification(doc, false); |
1382 |
} |
1383 |
} |
1384 |
|
1385 |
private int computeInsertionOffset(Caret caret) { |
1386 |
if (Utilities.isSelectionShowing(caret)) { |
1387 |
return Math.min(caret.getMark(), caret.getDot()); |
1388 |
} else { |
1389 |
return caret.getDot(); |
1390 |
} |
1391 |
} |
1392 |
|
1393 |
} // End of InsertBreakAction class |
1394 |
|
1395 |
@EditorActionRegistration(name = splitLineAction) |
1240 |
public static class SplitLineAction extends LocalBaseAction { |
1396 |
public static class SplitLineAction extends LocalBaseAction { |
1241 |
|
1397 |
|
1242 |
static final long serialVersionUID =7966576342334158659L; |
1398 |
static final long serialVersionUID =7966576342334158659L; |
Lines 1255-1278
Link Here
|
1255 |
final BaseDocument doc = (BaseDocument)target.getDocument(); |
1411 |
final BaseDocument doc = (BaseDocument)target.getDocument(); |
1256 |
final Caret caret = target.getCaret(); |
1412 |
final Caret caret = target.getCaret(); |
1257 |
|
1413 |
|
1258 |
final Formatter formatter = doc.getFormatter(); |
1414 |
final Indent formatter = Indent.get(doc); |
1259 |
formatter.indentLock(); |
1415 |
formatter.lock(); |
1260 |
try { |
1416 |
try { |
1261 |
doc.runAtomicAsUser (new Runnable () { |
1417 |
doc.runAtomicAsUser (new Runnable () { |
1262 |
public void run () { |
1418 |
public void run () { |
1263 |
DocumentUtilities.setTypingModification(doc, true); |
1419 |
DocumentUtilities.setTypingModification(doc, true); |
1264 |
try{ |
1420 |
try{ |
1265 |
target.replaceSelection(""); |
1421 |
target.replaceSelection(""); //NOI18N |
1266 |
final int dotPos = caret.getDot(); // dot stays where it was |
1422 |
|
1267 |
formatter.indentNewLine(doc, dotPos); // newline |
1423 |
// insert new line, caret stays where it is |
|
|
1424 |
int dotPos = caret.getDot(); |
1425 |
doc.insertString(dotPos, "\n", null); //NOI18N |
1426 |
|
1427 |
// reindent the new line |
1428 |
formatter.reindent(dotPos + 1); // newline |
1429 |
|
1430 |
// make sure the caret stays on its original position |
1268 |
caret.setDot(dotPos); |
1431 |
caret.setDot(dotPos); |
|
|
1432 |
} catch (BadLocationException ble) { |
1433 |
LOG.log(Level.WARNING, null, ble); |
1269 |
} finally { |
1434 |
} finally { |
1270 |
DocumentUtilities.setTypingModification(doc, false); |
1435 |
DocumentUtilities.setTypingModification(doc, false); |
1271 |
} |
1436 |
} |
1272 |
} |
1437 |
} |
1273 |
}); |
1438 |
}); |
1274 |
} finally { |
1439 |
} finally { |
1275 |
formatter.indentUnlock(); |
1440 |
formatter.unlock(); |
1276 |
} |
1441 |
} |
1277 |
} |
1442 |
} |
1278 |
} |
1443 |
} |
Lines 1300-1311
Link Here
|
1300 |
doc.runAtomicAsUser (new Runnable () { |
1465 |
doc.runAtomicAsUser (new Runnable () { |
1301 |
public void run () { |
1466 |
public void run () { |
1302 |
DocumentUtilities.setTypingModification(doc, true); |
1467 |
DocumentUtilities.setTypingModification(doc, true); |
1303 |
Formatter.pushFormattingContextDocument(doc); |
|
|
1304 |
try { |
1468 |
try { |
1305 |
if (Utilities.isSelectionShowing(caret)) { // block selected |
1469 |
if (Utilities.isSelectionShowing(caret)) { // block selected |
1306 |
try { |
1470 |
try { |
1307 |
doc.getFormatter().changeBlockIndent(doc, |
1471 |
changeBlockIndent(doc, target.getSelectionStart(), target.getSelectionEnd(), +1); |
1308 |
target.getSelectionStart(), target.getSelectionEnd(), +1); |
|
|
1309 |
} catch (GuardedException e) { |
1472 |
} catch (GuardedException e) { |
1310 |
target.getToolkit().beep(); |
1473 |
target.getToolkit().beep(); |
1311 |
} catch (BadLocationException e) { |
1474 |
} catch (BadLocationException e) { |
Lines 1341-1347
Link Here
|
1341 |
// Fix of #32240 - #1 of 2 |
1504 |
// Fix of #32240 - #1 of 2 |
1342 |
int rowStart = Utilities.getRowStart(doc, dotPos); |
1505 |
int rowStart = Utilities.getRowStart(doc, dotPos); |
1343 |
|
1506 |
|
1344 |
doc.getFormatter().changeRowIndent(doc, dotPos, indent); |
1507 |
changeRowIndent(doc, dotPos, indent); |
1345 |
|
1508 |
|
1346 |
// Fix of #32240 - #2 of 2 |
1509 |
// Fix of #32240 - #2 of 2 |
1347 |
int newDotPos = doc.getOffsetFromVisCol(indent, rowStart); |
1510 |
int newDotPos = doc.getOffsetFromVisCol(indent, rowStart); |
Lines 1350-1356
Link Here
|
1350 |
} |
1513 |
} |
1351 |
|
1514 |
|
1352 |
} else { // already chars on the line |
1515 |
} else { // already chars on the line |
1353 |
doc.getFormatter().insertTabString(doc, dotPos); |
1516 |
insertTabString(doc, dotPos); |
1354 |
|
1517 |
|
1355 |
} |
1518 |
} |
1356 |
} catch (BadLocationException e) { |
1519 |
} catch (BadLocationException e) { |
Lines 1358-1364
Link Here
|
1358 |
} |
1521 |
} |
1359 |
} |
1522 |
} |
1360 |
} finally { |
1523 |
} finally { |
1361 |
Formatter.popFormattingContextDocument(doc); |
|
|
1362 |
DocumentUtilities.setTypingModification(doc, false); |
1524 |
DocumentUtilities.setTypingModification(doc, false); |
1363 |
} |
1525 |
} |
1364 |
} |
1526 |
} |
Lines 1507-1513
Link Here
|
1507 |
} |
1669 |
} |
1508 |
} |
1670 |
} |
1509 |
|
1671 |
|
1510 |
/** Remove previous or next character */ |
1672 |
/** |
|
|
1673 |
* @deprecated Please do not subclass this class. Use Typing Hooks instead, for details see |
1674 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1675 |
*/ |
1511 |
public static class DeleteCharAction extends LocalBaseAction { |
1676 |
public static class DeleteCharAction extends LocalBaseAction { |
1512 |
|
1677 |
|
1513 |
protected boolean nextChar; |
1678 |
protected boolean nextChar; |
Lines 1531-1574
Link Here
|
1531 |
final int dot = caret.getDot(); |
1696 |
final int dot = caret.getDot(); |
1532 |
final int mark = caret.getMark(); |
1697 |
final int mark = caret.getMark(); |
1533 |
|
1698 |
|
1534 |
doc.runAtomicAsUser (new Runnable () { |
1699 |
if (dot != mark) { |
1535 |
public void run () { |
1700 |
// remove selection |
1536 |
DocumentUtilities.setTypingModification(doc, true); |
1701 |
doc.runAtomicAsUser (new Runnable () { |
1537 |
|
1702 |
public void run () { |
1538 |
try { |
1703 |
DocumentUtilities.setTypingModification(doc, true); |
1539 |
if (dot != mark) { // remove selection |
1704 |
try { |
1540 |
doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); |
1705 |
doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); |
1541 |
} else { |
1706 |
} catch (BadLocationException e) { |
1542 |
if (nextChar) { // remove next char |
1707 |
target.getToolkit().beep(); |
1543 |
char ch = doc.getChars(dot, 1)[0]; |
1708 |
} finally { |
1544 |
doc.remove(dot, 1); |
1709 |
DocumentUtilities.setTypingModification(doc, false); |
1545 |
charDeleted(doc, dot, caret, ch); |
|
|
1546 |
} else { // remove previous char |
1547 |
char ch = doc.getChars(dot-1, 1)[0]; |
1548 |
doc.remove(dot - 1, 1); |
1549 |
charBackspaced(doc, dot-1, caret, ch); |
1550 |
} |
1710 |
} |
1551 |
} |
1711 |
} |
1552 |
} catch (BadLocationException e) { |
1712 |
}); |
|
|
1713 |
} else { |
1714 |
char [] removedChar = null; |
1715 |
|
1716 |
try { |
1717 |
removedChar = nextChar ? |
1718 |
dot < doc.getLength() - 1 ? doc.getChars(dot, 1) : null : |
1719 |
dot > 0 ? doc.getChars(dot - 1, 1) : null; |
1720 |
} catch (BadLocationException ble) { |
1553 |
target.getToolkit().beep(); |
1721 |
target.getToolkit().beep(); |
1554 |
} finally { |
|
|
1555 |
DocumentUtilities.setTypingModification(doc, false); |
1556 |
} |
1722 |
} |
|
|
1723 |
|
1724 |
if (removedChar != null) { |
1725 |
final String removedText = String.valueOf(removedChar); |
1726 |
final DeletedTextInterceptorsManager.Transaction t = DeletedTextInterceptorsManager.getInstance().openTransaction(target, dot, removedText, !nextChar); |
1727 |
try { |
1728 |
if (!t.beforeRemove()) { |
1729 |
final boolean [] result = new boolean [] { false }; |
1730 |
doc.runAtomicAsUser (new Runnable () { |
1731 |
public void run () { |
1732 |
DocumentUtilities.setTypingModification(doc, true); |
1733 |
try { |
1734 |
if (nextChar) { // remove next char |
1735 |
doc.remove(dot, 1); |
1736 |
} else { // remove previous char |
1737 |
doc.remove(dot - 1, 1); |
1738 |
} |
1739 |
|
1740 |
t.textDeleted(); |
1741 |
|
1742 |
if (nextChar) { |
1743 |
charDeleted(doc, dot, caret, removedText.charAt(0)); |
1744 |
} else { |
1745 |
charBackspaced(doc, dot - 1, caret, removedText.charAt(0)); |
1746 |
} |
1747 |
|
1748 |
result[0] = true; |
1749 |
} catch (BadLocationException e) { |
1750 |
target.getToolkit().beep(); |
1751 |
} finally { |
1752 |
DocumentUtilities.setTypingModification(doc, false); |
1753 |
} |
1754 |
} |
1755 |
}); |
1756 |
|
1757 |
if (result[0]) { |
1758 |
t.afterRemove(); |
1759 |
} |
1760 |
} |
1761 |
} finally { |
1762 |
t.close(); |
1763 |
} |
1557 |
} |
1764 |
} |
1558 |
}); |
1765 |
} |
1559 |
} |
1766 |
} |
1560 |
} |
1767 |
} |
1561 |
|
1768 |
|
1562 |
protected void charBackspaced(BaseDocument doc, int dotPos, Caret caret, char ch) |
1769 |
/** |
1563 |
throws BadLocationException |
1770 |
* @deprecated Please use Typing Hooks instead, for details see |
1564 |
{ |
1771 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
1565 |
} |
1772 |
*/ |
1566 |
|
1773 |
protected void charBackspaced(BaseDocument doc, int dotPos, Caret caret, char ch) throws BadLocationException { |
1567 |
protected void charDeleted(BaseDocument doc, int dotPos, Caret caret, char ch) |
1774 |
} |
1568 |
throws BadLocationException |
1775 |
|
1569 |
{ |
1776 |
/** |
1570 |
} |
1777 |
* @deprecated Please use Typing Hooks instead, for details see |
1571 |
} |
1778 |
* <a href="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>. |
|
|
1779 |
*/ |
1780 |
protected void charDeleted(BaseDocument doc, int dotPos, Caret caret, char ch) throws BadLocationException { |
1781 |
} |
1782 |
} // End of DeleteCharAction class |
1572 |
|
1783 |
|
1573 |
/** |
1784 |
/** |
1574 |
* @deprecated this action is no longer used. It is reimplemented in editor.actions module. |
1785 |
* @deprecated this action is no longer used. It is reimplemented in editor.actions module. |
Lines 1734-1743
Link Here
|
1734 |
LOG.log(Level.WARNING, "Can't add position to the history of edits.", e); //NOI18N |
1945 |
LOG.log(Level.WARNING, "Can't add position to the history of edits.", e); //NOI18N |
1735 |
} |
1946 |
} |
1736 |
|
1947 |
|
1737 |
final Formatter formatter = doc.getFormatter(); |
1948 |
final Reformat formatter = Reformat.get(doc); |
1738 |
final boolean formatted = pasteFormatedAction.equals(getValue(Action.NAME)); |
1949 |
final boolean formatted = pasteFormatedAction.equals(getValue(Action.NAME)); |
1739 |
if (formatted) { |
1950 |
if (formatted) { |
1740 |
formatter.reformatLock(); |
1951 |
formatter.lock(); |
1741 |
} |
1952 |
} |
1742 |
try { |
1953 |
try { |
1743 |
doc.runAtomicAsUser (new Runnable () { |
1954 |
doc.runAtomicAsUser (new Runnable () { |
Lines 1758-1764
Link Here
|
1758 |
} |
1969 |
} |
1759 |
int endOffset = caret.getDot(); |
1970 |
int endOffset = caret.getDot(); |
1760 |
if (formatted) { |
1971 |
if (formatted) { |
1761 |
formatter.reformat(doc, startOffset, endOffset); |
1972 |
formatter.reformat(startOffset, endOffset); |
1762 |
} |
1973 |
} |
1763 |
} catch (Exception e) { |
1974 |
} catch (Exception e) { |
1764 |
target.getToolkit().beep(); |
1975 |
target.getToolkit().beep(); |
Lines 1769-1840
Link Here
|
1769 |
}); |
1980 |
}); |
1770 |
} finally { |
1981 |
} finally { |
1771 |
if (formatted) { |
1982 |
if (formatted) { |
1772 |
formatter.reformatUnlock(); |
1983 |
formatter.unlock(); |
1773 |
} |
1984 |
} |
1774 |
} |
1985 |
} |
1775 |
} |
1986 |
} |
1776 |
} |
1987 |
} |
1777 |
|
1988 |
|
1778 |
public static void indentBlock(BaseDocument doc, int startOffset, int endOffset) |
1989 |
// public static void indentBlock(BaseDocument doc, int startOffset, int endOffset) |
1779 |
throws BadLocationException |
1990 |
// throws BadLocationException |
1780 |
{ |
1991 |
// { |
1781 |
char [] text = doc.getChars(startOffset, endOffset-startOffset); |
1992 |
// char [] text = doc.getChars(startOffset, endOffset-startOffset); |
1782 |
String [] lines = toLines(new String(text)); |
1993 |
// String [] lines = toLines(new String(text)); |
1783 |
|
1994 |
// |
1784 |
doc.remove(startOffset, endOffset - startOffset); |
1995 |
// doc.remove(startOffset, endOffset - startOffset); |
1785 |
// System.out.println("Lines:\n"); // NOI18N |
1996 |
// // System.out.println("Lines:\n"); // NOI18N |
1786 |
// for (int j = 0 ; j < lines.length; j++) System.out.println(lines[j] + "<"); // NOI18N |
1997 |
// // for (int j = 0 ; j < lines.length; j++) System.out.println(lines[j] + "<"); // NOI18N |
1787 |
|
1998 |
// |
1788 |
int offset = startOffset; |
1999 |
// int offset = startOffset; |
1789 |
// handle the full lines |
2000 |
// // handle the full lines |
1790 |
for (int i = 0; i < lines.length - 1; i++) { |
2001 |
// for (int i = 0; i < lines.length - 1; i++) { |
1791 |
String indent = getIndentString(doc, offset, lines[i]); |
2002 |
// String indent = getIndentString(doc, offset, lines[i]); |
1792 |
String fragment = indent + lines[i].trim() + '\n'; |
2003 |
// String fragment = indent + lines[i].trim() + '\n'; |
1793 |
// System.out.println(fragment + "|"); // NOI18N |
2004 |
// // System.out.println(fragment + "|"); // NOI18N |
1794 |
doc.insertString(offset, fragment, null); |
2005 |
// doc.insertString(offset, fragment, null); |
1795 |
offset += fragment.length(); |
2006 |
// offset += fragment.length(); |
1796 |
} |
2007 |
// } |
1797 |
|
2008 |
// |
1798 |
// the rest just paste without indenting |
2009 |
// // the rest just paste without indenting |
1799 |
doc.insertString(offset, lines[lines.length-1], null); |
2010 |
// doc.insertString(offset, lines[lines.length-1], null); |
1800 |
|
2011 |
// |
1801 |
} |
2012 |
// } |
1802 |
|
2013 |
// |
1803 |
/** Break string to lines */ |
2014 |
// /** Break string to lines */ |
1804 |
private static String [] toLines(String str) { |
2015 |
// private static String [] toLines(String str) { |
1805 |
Vector v = new Vector(); |
2016 |
// Vector v = new Vector(); |
1806 |
int p=0 , p0=0; |
2017 |
// int p=0 , p0=0; |
1807 |
for (; p < str.length() ; p++) { |
2018 |
// for (; p < str.length() ; p++) { |
1808 |
if (str.charAt(p) == '\n') { |
2019 |
// if (str.charAt(p) == '\n') { |
1809 |
v.add(str.substring(p0, p+1)); |
2020 |
// v.add(str.substring(p0, p+1)); |
1810 |
p0 = p+1; |
2021 |
// p0 = p+1; |
1811 |
} |
2022 |
// } |
1812 |
} |
2023 |
// } |
1813 |
if (p0 < str.length()) v.add(str.substring(p0, str.length())); else v.add(""); |
2024 |
// if (p0 < str.length()) v.add(str.substring(p0, str.length())); else v.add(""); |
1814 |
|
2025 |
// |
1815 |
return (String [])v.toArray(new String [0]); |
2026 |
// return (String [])v.toArray(new String [0]); |
1816 |
} |
2027 |
// } |
1817 |
|
2028 |
// |
1818 |
private static String getIndentString(BaseDocument doc, int startOffset, String str) { |
2029 |
// private static String getIndentString(BaseDocument doc, int startOffset, String str) { |
1819 |
try { |
2030 |
// try { |
1820 |
Formatter f = doc.getFormatter(); |
2031 |
// Formatter f = doc.getFormatter(); |
1821 |
CharArrayWriter cw = new CharArrayWriter(); |
2032 |
// CharArrayWriter cw = new CharArrayWriter(); |
1822 |
Writer w = f.createWriter(doc, startOffset, cw); |
2033 |
// Writer w = f.createWriter(doc, startOffset, cw); |
1823 |
w.write(str, 0, str.length()); |
2034 |
// w.write(str, 0, str.length()); |
1824 |
w.close(); |
2035 |
// w.close(); |
1825 |
String out = new String(cw.toCharArray()); |
2036 |
// String out = new String(cw.toCharArray()); |
1826 |
int i = 0; |
2037 |
// int i = 0; |
1827 |
for (; i < out.length(); i++) { |
2038 |
// for (; i < out.length(); i++) { |
1828 |
if (out.charAt(i) != ' ' && out.charAt(i) != '\t') break; |
2039 |
// if (out.charAt(i) != ' ' && out.charAt(i) != '\t') break; |
1829 |
} |
2040 |
// } |
1830 |
// System.out.println(out+"|"); // NOI18N |
2041 |
// // System.out.println(out+"|"); // NOI18N |
1831 |
// System.out.println(out.substring(0,i)+"^"); // NOI18N |
2042 |
// // System.out.println(out.substring(0,i)+"^"); // NOI18N |
1832 |
|
2043 |
// |
1833 |
return out.substring(0, i); |
2044 |
// return out.substring(0, i); |
1834 |
} catch (java.io.IOException e) { |
2045 |
// } catch (java.io.IOException e) { |
1835 |
return ""; |
2046 |
// return ""; |
1836 |
} |
2047 |
// } |
1837 |
} |
2048 |
// } |
1838 |
} |
2049 |
} |
1839 |
|
2050 |
|
1840 |
|
2051 |
|
Lines 2875-2878
Link Here
|
2875 |
|
3086 |
|
2876 |
} |
3087 |
} |
2877 |
|
3088 |
|
|
|
3089 |
|
3090 |
/** Modify the line to move the text starting at dotPos one tab |
3091 |
* column to the right. Whitespace preceeding dotPos may be |
3092 |
* replaced by a TAB character if tabs expanding is on. |
3093 |
* @param doc document to operate on |
3094 |
* @param dotPos insertion point |
3095 |
*/ |
3096 |
static void insertTabString (final BaseDocument doc, final int dotPos) throws BadLocationException { |
3097 |
final BadLocationException[] badLocationExceptions = new BadLocationException [1]; |
3098 |
doc.runAtomic (new Runnable () { |
3099 |
public void run () { |
3100 |
try { |
3101 |
// Determine first white char before dotPos |
3102 |
int rsPos = Utilities.getRowStart(doc, dotPos); |
3103 |
int startPos = Utilities.getFirstNonWhiteBwd(doc, dotPos, rsPos); |
3104 |
startPos = (startPos >= 0) ? (startPos + 1) : rsPos; |
3105 |
|
3106 |
int startCol = Utilities.getVisualColumn(doc, startPos); |
3107 |
int endCol = Utilities.getNextTabColumn(doc, dotPos); |
3108 |
Preferences prefs = CodeStylePreferences.get(doc).getPreferences(); |
3109 |
String tabStr = Analyzer.getWhitespaceString( |
3110 |
startCol, endCol, |
3111 |
prefs.getBoolean(SimpleValueNames.EXPAND_TABS, EditorPreferencesDefaults.defaultExpandTabs), |
3112 |
prefs.getInt(SimpleValueNames.TAB_SIZE, EditorPreferencesDefaults.defaultTabSize)); |
3113 |
|
3114 |
// Search for the first non-common char |
3115 |
char[] removeChars = doc.getChars(startPos, dotPos - startPos); |
3116 |
int ind = 0; |
3117 |
while (ind < removeChars.length && removeChars[ind] == tabStr.charAt(ind)) { |
3118 |
ind++; |
3119 |
} |
3120 |
|
3121 |
startPos += ind; |
3122 |
doc.remove(startPos, dotPos - startPos); |
3123 |
doc.insertString(startPos, tabStr.substring(ind), null); |
3124 |
} catch (BadLocationException ex) { |
3125 |
badLocationExceptions [0] = ex; |
3126 |
} |
3127 |
} |
3128 |
}); |
3129 |
if (badLocationExceptions[0] != null) |
3130 |
throw badLocationExceptions [0]; |
3131 |
} |
3132 |
|
3133 |
/** Change the indent of the given row. Document is atomically locked |
3134 |
* during this operation. |
3135 |
*/ |
3136 |
static void changeRowIndent (final BaseDocument doc, final int pos, final int newIndent) throws BadLocationException { |
3137 |
final BadLocationException[] badLocationExceptions = new BadLocationException [1]; |
3138 |
doc.runAtomic (new Runnable () { |
3139 |
public void run () { |
3140 |
try { |
3141 |
int indent = newIndent < 0 ? 0 : newIndent; |
3142 |
int firstNW = Utilities.getRowFirstNonWhite(doc, pos); |
3143 |
if (firstNW == -1) { // valid first non-blank |
3144 |
firstNW = Utilities.getRowEnd(doc, pos); |
3145 |
} |
3146 |
int replacePos = Utilities.getRowStart(doc, pos); |
3147 |
int removeLen = firstNW - replacePos; |
3148 |
CharSequence removeText = DocumentUtilities.getText(doc, replacePos, removeLen); |
3149 |
String newIndentText = IndentUtils.createIndentString(doc, indent); |
3150 |
if (CharSequenceUtilities.startsWith(newIndentText, removeText)) { |
3151 |
// Skip removeLen chars at start |
3152 |
newIndentText = newIndentText.substring(removeLen); |
3153 |
replacePos += removeLen; |
3154 |
removeLen = 0; |
3155 |
} else if (CharSequenceUtilities.endsWith(newIndentText, removeText)) { |
3156 |
// Skip removeLen chars at the end |
3157 |
newIndentText = newIndentText.substring(0, newIndentText.length() - removeLen); |
3158 |
removeLen = 0; |
3159 |
} |
3160 |
|
3161 |
if (removeLen != 0) { |
3162 |
doc.remove(replacePos, removeLen); |
3163 |
} |
3164 |
|
3165 |
doc.insertString(replacePos, newIndentText, null); |
3166 |
} catch (BadLocationException ex) { |
3167 |
badLocationExceptions [0] = ex; |
3168 |
} |
3169 |
} |
3170 |
}); |
3171 |
if (badLocationExceptions[0] != null) |
3172 |
throw badLocationExceptions [0]; |
3173 |
} |
3174 |
|
3175 |
/** Increase/decrease indentation of the block of the code. Document |
3176 |
* is atomically locked during the operation. |
3177 |
* @param doc document to operate on |
3178 |
* @param startPos starting line position |
3179 |
* @param endPos ending line position |
3180 |
* @param shiftCnt positive/negative count of shiftwidths by which indentation |
3181 |
* should be shifted right/left |
3182 |
*/ |
3183 |
static void changeBlockIndent (final BaseDocument doc, final int startPos, final int endPos, |
3184 |
final int shiftCnt) throws BadLocationException { |
3185 |
GuardedDocument gdoc = (doc instanceof GuardedDocument) |
3186 |
? (GuardedDocument)doc : null; |
3187 |
if (gdoc != null){ |
3188 |
for (int i = startPos; i<endPos; i++){ |
3189 |
if (gdoc.isPosGuarded(i)){ |
3190 |
java.awt.Toolkit.getDefaultToolkit().beep(); |
3191 |
return; |
3192 |
} |
3193 |
} |
3194 |
} |
3195 |
|
3196 |
final BadLocationException[] badLocationExceptions = new BadLocationException [1]; |
3197 |
doc.runAtomic (new Runnable () { |
3198 |
public void run () { |
3199 |
try { |
3200 |
int indentDelta = shiftCnt * doc.getShiftWidth(); |
3201 |
int end = (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) ? |
3202 |
endPos - 1 : endPos; |
3203 |
|
3204 |
int pos = Utilities.getRowStart(doc, startPos ); |
3205 |
for (int lineCnt = Utilities.getRowCount(doc, startPos, end); |
3206 |
lineCnt > 0; lineCnt-- |
3207 |
) { |
3208 |
int indent = Utilities.getRowIndent(doc, pos); |
3209 |
if (Utilities.isRowWhite(doc, pos)) { |
3210 |
indent = -indentDelta; // zero indentation for white line |
3211 |
} |
3212 |
changeRowIndent(doc, pos, Math.max(indent + indentDelta, 0)); |
3213 |
pos = Utilities.getRowStart(doc, pos, +1); |
3214 |
} |
3215 |
} catch (BadLocationException ex) { |
3216 |
badLocationExceptions [0] = ex; |
3217 |
} |
3218 |
} |
3219 |
}); |
3220 |
if (badLocationExceptions[0] != null) |
3221 |
throw badLocationExceptions [0]; |
3222 |
} |
3223 |
|
3224 |
/** Shift line either left or right */ |
3225 |
static void shiftLine(BaseDocument doc, int dotPos, boolean right) throws BadLocationException { |
3226 |
int ind = doc.getShiftWidth(); |
3227 |
if (!right) { |
3228 |
ind = -ind; |
3229 |
} |
3230 |
|
3231 |
if (Utilities.isRowWhite(doc, dotPos)) { |
3232 |
ind += Utilities.getVisualColumn(doc, dotPos); |
3233 |
} else { |
3234 |
ind += Utilities.getRowIndent(doc, dotPos); |
3235 |
} |
3236 |
ind = Math.max(ind, 0); |
3237 |
changeRowIndent(doc, dotPos, ind); |
3238 |
} |
2878 |
} |
3239 |
} |