Browse Source

报表REPORT-20320 sonar问题修复

research/11.0
vito 5 years ago
parent
commit
7937140272
  1. 110
      designer-base/src/main/java/com/fr/design/gui/syntax/ui/rsyntaxtextarea/RSyntaxTextAreaEditorKit.java
  2. 173
      designer-base/src/main/java/com/fr/design/gui/syntax/ui/rsyntaxtextarea/RSyntaxUtilities.java

110
designer-base/src/main/java/com/fr/design/gui/syntax/ui/rsyntaxtextarea/RSyntaxTextAreaEditorKit.java

@ -8,23 +8,33 @@
*/ */
package com.fr.design.gui.syntax.ui.rsyntaxtextarea; package com.fr.design.gui.syntax.ui.rsyntaxtextarea;
import java.awt.*;
import java.awt.event.*;
import java.util.ResourceBundle;
import java.util.Stack;
import javax.swing.*;
import javax.swing.text.*;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.Fold; import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.Fold;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.FoldCollapser; import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.FoldCollapser;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.FoldManager; import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.FoldManager;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.templates.CodeTemplate; import com.fr.design.gui.syntax.ui.rsyntaxtextarea.templates.CodeTemplate;
import com.fr.design.gui.syntax.ui.rtextarea.Gutter; import com.fr.design.gui.syntax.ui.rtextarea.Gutter;
import com.fr.design.gui.syntax.ui.rtextarea.IconRowHeader; import com.fr.design.gui.syntax.ui.rtextarea.IconRowHeader;
import com.fr.design.gui.syntax.ui.rtextarea.RecordableTextAction;
import com.fr.design.gui.syntax.ui.rtextarea.RTextArea; import com.fr.design.gui.syntax.ui.rtextarea.RTextArea;
import com.fr.design.gui.syntax.ui.rtextarea.RTextAreaEditorKit; import com.fr.design.gui.syntax.ui.rtextarea.RTextAreaEditorKit;
import com.fr.design.gui.syntax.ui.rtextarea.RecordableTextAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.Segment;
import javax.swing.text.TextAction;
import java.awt.Component;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.util.Objects;
import java.util.Stack;
/** /**
@ -269,8 +279,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
fold.setCollapsed(collapse); fold.setCollapsed(collapse);
} }
possiblyRepaintGutter(textArea); possiblyRepaintGutter(textArea);
} } else {
else {
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
} }
} }
@ -473,15 +482,13 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
} }
t = t.getNextToken(); t = t.getNextToken();
} }
} } else if (t.length() == 2 && t.charAt(0) == '/' &&
else if (t.length()==2 && t.charAt(0)=='/' &&
(t.charAt(1) == '>' || (t.charAt(1) == '>' ||
t.charAt(1) == ']')) { t.charAt(1) == ']')) {
if (!stack.isEmpty()) { // Always true for valid XML if (!stack.isEmpty()) { // Always true for valid XML
stack.pop(); stack.pop();
} }
} } else if (t.length() == 2 &&
else if (t.length()==2 &&
(t.charAt(0) == '<' || t.charAt(0) == '[') && (t.charAt(0) == '<' || t.charAt(0) == '[') &&
t.charAt(1) == '/') { t.charAt(1) == '/') {
String tagName = null; String tagName = null;
@ -494,7 +501,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
} }
} }
t = t.getNextToken(); t = Objects.requireNonNull(t).getNextToken();
} }
@ -536,8 +543,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
FoldCollapser collapser = new FoldCollapser(); FoldCollapser collapser = new FoldCollapser();
collapser.collapseFolds(rsta.getFoldManager()); collapser.collapseFolds(rsta.getFoldManager());
possiblyRepaintGutter(textArea); possiblyRepaintGutter(textArea);
} } else {
else {
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
} }
} }
@ -585,8 +591,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
}; };
collapser.collapseFolds(rsta.getFoldManager()); collapser.collapseFolds(rsta.getFoldManager());
possiblyRepaintGutter(textArea); possiblyRepaintGutter(textArea);
} } else {
else {
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
} }
} }
@ -671,8 +676,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
// Shrink by decreaseAmount. // Shrink by decreaseAmount.
ss.font = font.deriveFont(newSize); ss.font = font.deriveFont(newSize);
changed = true; changed = true;
} } else if (oldSize > MINIMUM_SIZE) {
else if (oldSize>MINIMUM_SIZE) {
// Can't shrink by full decreaseAmount, but // Can't shrink by full decreaseAmount, but
// can shrink a little bit. // can shrink a little bit.
ss.font = font.deriveFont(MINIMUM_SIZE); ss.font = font.deriveFont(MINIMUM_SIZE);
@ -690,8 +694,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
// Shrink by decreaseAmount. // Shrink by decreaseAmount.
rsta.setFont(font.deriveFont(newSize)); rsta.setFont(font.deriveFont(newSize));
changed = true; changed = true;
} } else if (oldSize > MINIMUM_SIZE) {
else if (oldSize>MINIMUM_SIZE) {
// Can't shrink by full decreaseAmount, but // Can't shrink by full decreaseAmount, but
// can shrink a little bit. // can shrink a little bit.
rsta.setFont(font.deriveFont(MINIMUM_SIZE)); rsta.setFont(font.deriveFont(MINIMUM_SIZE));
@ -717,9 +720,9 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
parent.repaint(); parent.repaint();
} }
} }
} } else {
else
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
}
} }
@ -789,8 +792,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
UIManager.getLookAndFeel(). UIManager.getLookAndFeel().
provideErrorFeedback(textArea); provideErrorFeedback(textArea);
} }
} } else {
else {
Element elem = map.getElement(line1); Element elem = map.getElement(line1);
try { try {
handleDecreaseIndent(elem, document, tabSize); handleDecreaseIndent(elem, document, tabSize);
@ -1038,8 +1040,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
expand(fm.getFold(i)); expand(fm.getFold(i));
} }
possiblyRepaintGutter(rsta); possiblyRepaintGutter(rsta);
} } else {
else {
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
} }
} }
@ -1128,8 +1129,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
// Go to the position AFTER the bracket so the previous // Go to the position AFTER the bracket so the previous
// bracket (which we were just on) is highlighted. // bracket (which we were just on) is highlighted.
rsta.setCaretPosition(bracketInfo.y + 1); rsta.setCaretPosition(bracketInfo.y + 1);
} } else {
else {
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
} }
} }
@ -1184,8 +1184,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
// Grow by increaseAmount. // Grow by increaseAmount.
ss.font = font.deriveFont(newSize); ss.font = font.deriveFont(newSize);
changed = true; changed = true;
} } else if (oldSize < MAXIMUM_SIZE) {
else if (oldSize<MAXIMUM_SIZE) {
// Can't grow by full increaseAmount, but // Can't grow by full increaseAmount, but
// can grow a little bit. // can grow a little bit.
ss.font = font.deriveFont(MAXIMUM_SIZE); ss.font = font.deriveFont(MAXIMUM_SIZE);
@ -1203,8 +1202,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
// Grow by increaseAmount. // Grow by increaseAmount.
rsta.setFont(font.deriveFont(newSize)); rsta.setFont(font.deriveFont(newSize));
changed = true; changed = true;
} } else if (oldSize < MAXIMUM_SIZE) {
else if (oldSize<MAXIMUM_SIZE) {
// Can't grow by full increaseAmount, but // Can't grow by full increaseAmount, but
// can grow a little bit. // can grow a little bit.
rsta.setFont(font.deriveFont(MAXIMUM_SIZE)); rsta.setFont(font.deriveFont(MAXIMUM_SIZE));
@ -1230,9 +1228,9 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
parent.repaint(); parent.repaint();
} }
} }
} } else {
else
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
}
} }
@ -1282,9 +1280,10 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
*/ */
private static final int atEndOfLine(int pos, String s, int sLen) { private static final int atEndOfLine(int pos, String s, int sLen) {
for (int i = pos; i < sLen; i++) { for (int i = pos; i < sLen; i++) {
if (!RSyntaxUtilities.isWhitespace(s.charAt(i))) if (!RSyntaxUtilities.isWhitespace(s.charAt(i))) {
return i; return i;
} }
}
return -1; return -1;
} }
@ -1299,8 +1298,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
char ch = t.charAt(0); char ch = t.charAt(0);
if (ch == '{') { if (ch == '{') {
openCount++; openCount++;
} } else if (ch == '}') {
else if (ch=='}') {
openCount--; openCount--;
} }
} }
@ -1323,8 +1321,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
// If we're auto-indenting... // If we're auto-indenting...
if (noSelection && textArea.isAutoIndentEnabled()) { if (noSelection && textArea.isAutoIndentEnabled()) {
insertNewlineWithAutoIndent(textArea); insertNewlineWithAutoIndent(textArea);
} } else {
else {
textArea.replaceSelection("\n"); textArea.replaceSelection("\n");
if (noSelection) { if (noSelection) {
possiblyCloseCurlyBrace(textArea, null); possiblyCloseCurlyBrace(textArea, null);
@ -1472,7 +1469,8 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
if (dotLine != markLine) { if (dotLine != markLine) {
int first = Math.min(dotLine, markLine); int first = Math.min(dotLine, markLine);
int last = Math.max(dotLine, markLine); int last = Math.max(dotLine, markLine);
Element elem; int start; Element elem;
int start;
// Since we're using Document.insertString(), we must mimic the // Since we're using Document.insertString(), we must mimic the
// soft tab behavior provided by RTextArea.replaceSelection(). // soft tab behavior provided by RTextArea.replaceSelection().
@ -1509,8 +1507,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
} finally { } finally {
textArea.endAtomicEdit(); textArea.endAtomicEdit();
} }
} } else {
else {
textArea.replaceSelection("\t"); textArea.replaceSelection("\t");
} }
@ -1559,14 +1556,15 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
if (rsta.isCodeFoldingEnabled()) { // Start of next visible line if (rsta.isCodeFoldingEnabled()) { // Start of next visible line
FoldManager fm = rsta.getFoldManager(); FoldManager fm = rsta.getFoldManager();
int lineCount = root.getElementCount(); int lineCount = root.getElementCount();
while (++line<lineCount && fm.isLineHidden(line)); while (++line < lineCount && fm.isLineHidden(line)) {
;
}
if (line < lineCount) { // Found a lower visible line if (line < lineCount) { // Found a lower visible line
offs = root.getElement(line).getStartOffset(); offs = root.getElement(line).getStartOffset();
} }
// No lower visible line - we're already at last visible offset // No lower visible line - we're already at last visible offset
return offs; return offs;
} } else {
else {
return offs + 1; // Start of next line. return offs + 1; // Start of next line.
} }
} }
@ -1621,8 +1619,9 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
@Override @Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) if (!textArea.isEditable() || !textArea.isEnabled()) {
return; return;
}
RSyntaxTextArea rsta = (RSyntaxTextArea) textArea; RSyntaxTextArea rsta = (RSyntaxTextArea) textArea;
@ -1726,14 +1725,15 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
RSyntaxTextArea rsta = (RSyntaxTextArea) textArea; RSyntaxTextArea rsta = (RSyntaxTextArea) textArea;
if (rsta.isCodeFoldingEnabled()) { // End of next visible line if (rsta.isCodeFoldingEnabled()) { // End of next visible line
FoldManager fm = rsta.getFoldManager(); FoldManager fm = rsta.getFoldManager();
while (--line>=0 && fm.isLineHidden(line)); while (--line >= 0 && fm.isLineHidden(line)) {
;
}
if (line >= 0) { // Found an earlier visible line if (line >= 0) { // Found an earlier visible line
offs = root.getElement(line).getEndOffset() - 1; offs = root.getElement(line).getEndOffset() - 1;
} }
// No earlier visible line - we must be at offs==0... // No earlier visible line - we must be at offs==0...
return offs; return offs;
} } else {
else {
return start - 1; // End of previous line. return start - 1; // End of previous line.
} }
} }
@ -1879,8 +1879,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
doc.insertString(end + startEnd[0].length(), startEnd[1], doc.insertString(end + startEnd[0].length(), startEnd[1],
null); null);
} }
} } else {
else {
doc.remove(start, startEnd[0].length()); doc.remove(start, startEnd[0].length());
if (startEnd[1] != null) { if (startEnd[1] != null) {
int temp = startEnd[1].length(); int temp = startEnd[1].length();
@ -1923,8 +1922,7 @@ public class RSyntaxTextAreaEditorKit extends RTextAreaEditorKit {
fold.toggleCollapsedState(); fold.toggleCollapsedState();
} }
possiblyRepaintGutter(textArea); possiblyRepaintGutter(textArea);
} } else {
else {
UIManager.getLookAndFeel().provideErrorFeedback(rsta); UIManager.getLookAndFeel().provideErrorFeedback(rsta);
} }
} }

173
designer-base/src/main/java/com/fr/design/gui/syntax/ui/rsyntaxtextarea/RSyntaxUtilities.java

@ -9,6 +9,24 @@
*/ */
package com.fr.design.gui.syntax.ui.rsyntaxtextarea; package com.fr.design.gui.syntax.ui.rsyntaxtextarea;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.TokenUtils.TokenSubList;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.FoldManager;
import com.fr.design.gui.syntax.ui.rtextarea.Gutter;
import com.fr.design.gui.syntax.ui.rtextarea.RTextArea;
import com.fr.design.gui.syntax.ui.rtextarea.RTextScrollPane;
import javax.swing.JLabel;
import javax.swing.JViewport;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.Position;
import javax.swing.text.Segment;
import javax.swing.text.TabExpander;
import javax.swing.text.View;
import java.awt.Color; import java.awt.Color;
import java.awt.Container; import java.awt.Container;
import java.awt.Point; import java.awt.Point;
@ -16,23 +34,9 @@ import java.awt.Rectangle;
import java.awt.Shape; import java.awt.Shape;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.Position;
import javax.swing.text.Segment;
import javax.swing.text.TabExpander;
import javax.swing.text.View;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.TokenUtils.TokenSubList;
import com.fr.design.gui.syntax.ui.rsyntaxtextarea.folding.FoldManager;
import com.fr.design.gui.syntax.ui.rtextarea.Gutter;
import com.fr.design.gui.syntax.ui.rtextarea.RTextArea;
import com.fr.design.gui.syntax.ui.rtextarea.RTextScrollPane;
/** /**
@ -163,8 +167,7 @@ public class RSyntaxUtilities implements SwingConstants {
case ' ': case ' ':
if (inPreBlock || !lastWasSpace) { if (inPreBlock || !lastWasSpace) {
sb.append(' '); sb.append(' ');
} } else {
else {
sb.append("&nbsp;"); sb.append("&nbsp;");
} }
lastWasSpace = true; lastWasSpace = true;
@ -361,10 +364,11 @@ public class RSyntaxUtilities implements SwingConstants {
RSyntaxDocument doc = (RSyntaxDocument) textArea.getDocument(); RSyntaxDocument doc = (RSyntaxDocument) textArea.getDocument();
// Ensure p0 and p1 are valid document positions. // Ensure p0 and p1 are valid document positions.
if (p0<0) if (p0 < 0) {
throw new BadLocationException("Invalid document position", p0); throw new BadLocationException("Invalid document position", p0);
else if (p1>doc.getLength()) } else if (p1 > doc.getLength()) {
throw new BadLocationException("Invalid document position", p1); throw new BadLocationException("Invalid document position", p1);
}
// Ensure p0 and p1 are in the same line, and get the start/end // Ensure p0 and p1 are in the same line, and get the start/end
// offsets for that line. // offsets for that line.
@ -373,9 +377,10 @@ public class RSyntaxUtilities implements SwingConstants {
// We do ">1" because p1 might be the first position on the next line // We do ">1" because p1 might be the first position on the next line
// or the last position on the previous one. // or the last position on the previous one.
// if (lineNum!=map.getElementIndex(p1)) // if (lineNum!=map.getElementIndex(p1))
if (Math.abs(lineNum-map.getElementIndex(p1))>1) if (Math.abs(lineNum - map.getElementIndex(p1)) > 1) {
throw new IllegalArgumentException("p0 and p1 are not on the " + throw new IllegalArgumentException("p0 and p1 are not on the " +
"same line (" + p0 + ", " + p1 + ")."); "same line (" + p0 + ", " + p1 + ").");
}
// Get the token list. // Get the token list.
Token t = doc.getTokenListForLine(lineNum); Token t = doc.getTokenListForLine(lineNum);
@ -455,14 +460,13 @@ public class RSyntaxUtilities implements SwingConstants {
Token token = doc.getTokenListForLine(curLine); Token token = doc.getTokenListForLine(curLine);
token = RSyntaxUtilities.getTokenAtOffset(token, caretPosition); token = RSyntaxUtilities.getTokenAtOffset(token, caretPosition);
// All brackets are always returned as "separators." // All brackets are always returned as "separators."
if (token.getType()!=Token.SEPARATOR) { if (Objects.requireNonNull(token).getType() != Token.SEPARATOR) {
return input; return input;
} }
if (index < 3) { // One of "{[(" if (index < 3) { // One of "{[("
goForward = true; goForward = true;
bracketMatch = BRACKETS.charAt(index + 3); bracketMatch = BRACKETS.charAt(index + 3);
} } else { // One of ")]}"
else { // One of ")]}"
goForward = false; goForward = false;
bracketMatch = BRACKETS.charAt(index - 3); bracketMatch = BRACKETS.charAt(index - 3);
} }
@ -487,24 +491,23 @@ public class RSyntaxUtilities implements SwingConstants {
char ch = charSegment.array[i]; char ch = charSegment.array[i];
if (ch == bracket) { if (ch == bracket) {
if (haveTokenList==false) { if (!haveTokenList) {
token = doc.getTokenListForLine(curLine); token = doc.getTokenListForLine(curLine);
haveTokenList = true; haveTokenList = true;
} }
int offset = start + (i - segOffset); int offset = start + (i - segOffset);
token = RSyntaxUtilities.getTokenAtOffset(token, offset); token = RSyntaxUtilities.getTokenAtOffset(token, offset);
if (token.getType()==Token.SEPARATOR) if (Objects.requireNonNull(token).getType() == Token.SEPARATOR) {
numEmbedded++; numEmbedded++;
} }
} else if (ch == bracketMatch) {
else if (ch==bracketMatch) { if (!haveTokenList) {
if (haveTokenList==false) {
token = doc.getTokenListForLine(curLine); token = doc.getTokenListForLine(curLine);
haveTokenList = true; haveTokenList = true;
} }
int offset = start + (i - segOffset); int offset = start + (i - segOffset);
token = RSyntaxUtilities.getTokenAtOffset(token, offset); token = RSyntaxUtilities.getTokenAtOffset(token, offset);
if (token.getType()==Token.SEPARATOR) { if (Objects.requireNonNull(token).getType() == Token.SEPARATOR) {
if (numEmbedded == 0) { if (numEmbedded == 0) {
if (textArea.isCodeFoldingEnabled() && if (textArea.isCodeFoldingEnabled() &&
textArea.getFoldManager().isLineHidden(curLine)) { textArea.getFoldManager().isLineHidden(curLine)) {
@ -521,8 +524,9 @@ public class RSyntaxUtilities implements SwingConstants {
// Bail out if we've gone through all lines and // Bail out if we've gone through all lines and
// haven't found the match. // haven't found the match.
if (++curLine==lastLine) if (++curLine == lastLine) {
return input; return input;
}
// Otherwise, go through the next line. // Otherwise, go through the next line.
haveTokenList = false; haveTokenList = false;
@ -557,24 +561,23 @@ public class RSyntaxUtilities implements SwingConstants {
char ch = charSegment.array[i]; char ch = charSegment.array[i];
if (ch == bracket) { if (ch == bracket) {
if (haveTokenList==false) { if (!haveTokenList) {
token = doc.getTokenListForLine(curLine); token = doc.getTokenListForLine(curLine);
haveTokenList = true; haveTokenList = true;
} }
int offset = start + (i - segOffset); int offset = start + (i - segOffset);
t2 = RSyntaxUtilities.getTokenAtOffset(token, offset); t2 = RSyntaxUtilities.getTokenAtOffset(token, offset);
if (t2.getType()==Token.SEPARATOR) if (Objects.requireNonNull(t2).getType() == Token.SEPARATOR) {
numEmbedded++; numEmbedded++;
} }
} else if (ch == bracketMatch) {
else if (ch==bracketMatch) { if (!haveTokenList) {
if (haveTokenList==false) {
token = doc.getTokenListForLine(curLine); token = doc.getTokenListForLine(curLine);
haveTokenList = true; haveTokenList = true;
} }
int offset = start + (i - segOffset); int offset = start + (i - segOffset);
t2 = RSyntaxUtilities.getTokenAtOffset(token, offset); t2 = RSyntaxUtilities.getTokenAtOffset(token, offset);
if (t2.getType()==Token.SEPARATOR) { if (Objects.requireNonNull(t2).getType() == Token.SEPARATOR) {
if (numEmbedded == 0) { if (numEmbedded == 0) {
input.setLocation(caretPosition, offset); input.setLocation(caretPosition, offset);
return input; return input;
@ -642,7 +645,7 @@ public class RSyntaxUtilities implements SwingConstants {
* Some views may not be visible, * Some views may not be visible,
* they might not be in the same order found in the model, or they just * they might not be in the same order found in the model, or they just
* might not allow access to some of the locations in the model.<p> * might not allow access to some of the locations in the model.<p>
* * <p>
* NOTE: You should only call this method if the passed-in * NOTE: You should only call this method if the passed-in
* <code>javax.swing.text.View</code> is an instance of * <code>javax.swing.text.View</code> is an instance of
* {@link TokenOrientedView} and <code>javax.swing.text.TabExpander</code>; * {@link TokenOrientedView} and <code>javax.swing.text.TabExpander</code>;
@ -661,8 +664,8 @@ public class RSyntaxUtilities implements SwingConstants {
* </ul> * </ul>
* @return the location within the model that best represents the next * @return the location within the model that best represents the next
* location visual position * location visual position
* @exception BadLocationException * @throws BadLocationException
* @exception IllegalArgumentException if <code>direction</code> * @throws IllegalArgumentException if <code>direction</code>
* doesn't have one of the legal values above * doesn't have one of the legal values above
*/ */
public static int getNextVisualPositionFrom(int pos, Position.Bias b, public static int getNextVisualPositionFrom(int pos, Position.Bias b,
@ -688,29 +691,29 @@ public class RSyntaxUtilities implements SwingConstants {
// YECK! Ideally, the x location from the magic caret // YECK! Ideally, the x location from the magic caret
// position would be passed in. // position would be passed in.
Point mcp; Point mcp;
if (c != null) if (c != null) {
mcp = c.getMagicCaretPosition(); mcp = c.getMagicCaretPosition();
else } else {
mcp = null; mcp = null;
}
int x; int x;
if (mcp == null) { if (mcp == null) {
Rectangle loc = target.modelToView(pos); Rectangle loc = Objects.requireNonNull(target).modelToView(pos);
x = (loc == null) ? 0 : loc.x; x = (loc == null) ? 0 : loc.x;
} } else {
else {
x = mcp.x; x = mcp.x;
} }
if (direction == NORTH) if (direction == NORTH) {
pos = getPositionAbove(target, pos, x, (TabExpander) view); pos = getPositionAbove(target, pos, x, (TabExpander) view);
else } else {
pos = getPositionBelow(target, pos, x, (TabExpander) view); pos = getPositionBelow(target, pos, x, (TabExpander) view);
}
break; break;
case WEST: case WEST:
if (pos == -1) { if (pos == -1) {
pos = Math.max(0, view.getEndOffset() - 1); pos = Math.max(0, view.getEndOffset() - 1);
} } else {
else {
pos = Math.max(0, pos - 1); pos = Math.max(0, pos - 1);
if (target.isCodeFoldingEnabled()) { if (target.isCodeFoldingEnabled()) {
int last = target.getLineOfOffset(pos + 1); int last = target.getLineOfOffset(pos + 1);
@ -718,7 +721,9 @@ public class RSyntaxUtilities implements SwingConstants {
if (last != current) { // If moving up a line... if (last != current) { // If moving up a line...
FoldManager fm = target.getFoldManager(); FoldManager fm = target.getFoldManager();
if (fm.isLineHidden(current)) { if (fm.isLineHidden(current)) {
while (--current>0 && fm.isLineHidden(current)); while (--current > 0 && fm.isLineHidden(current)) {
;
}
pos = target.getLineEndOffset(current) - 1; pos = target.getLineEndOffset(current) - 1;
} }
} }
@ -729,8 +734,7 @@ public class RSyntaxUtilities implements SwingConstants {
case EAST: case EAST:
if (pos == -1) { if (pos == -1) {
pos = view.getStartOffset(); pos = view.getStartOffset();
} } else {
else {
pos = Math.min(pos + 1, view.getDocument().getLength()); pos = Math.min(pos + 1, view.getDocument().getLength());
if (target.isCodeFoldingEnabled()) { if (target.isCodeFoldingEnabled()) {
int last = target.getLineOfOffset(pos - 1); int last = target.getLineOfOffset(pos - 1);
@ -739,7 +743,9 @@ public class RSyntaxUtilities implements SwingConstants {
FoldManager fm = target.getFoldManager(); FoldManager fm = target.getFoldManager();
if (fm.isLineHidden(current)) { if (fm.isLineHidden(current)) {
int lineCount = target.getLineCount(); int lineCount = target.getLineCount();
while (++current<lineCount && fm.isLineHidden(current)); while (++current < lineCount && fm.isLineHidden(current)) {
;
}
pos = current == lineCount ? pos = current == lineCount ?
target.getLineEndOffset(last) - 1 : // Was the last visible line target.getLineEndOffset(last) - 1 : // Was the last visible line
target.getLineStartOffset(current); target.getLineStartOffset(current);
@ -770,23 +776,22 @@ public class RSyntaxUtilities implements SwingConstants {
* @param x the X coordinate >= 0 * @param x the X coordinate >= 0
* @return the position >= 0 if the request can be computed, otherwise * @return the position >= 0 if the request can be computed, otherwise
* a value of -1 will be returned. * a value of -1 will be returned.
* @exception BadLocationException if the offset is out of range * @throws BadLocationException if the offset is out of range
*/ */
public static final int getPositionAbove(RSyntaxTextArea c, int offs, public static final int getPositionAbove(RSyntaxTextArea c, int offs,
float x, TabExpander e) throws BadLocationException { float x, TabExpander e) throws BadLocationException {
TokenOrientedView tov = (TokenOrientedView) e; TokenOrientedView tov = (TokenOrientedView) e;
Token token = tov.getTokenListForPhysicalLineAbove(offs); Token token = tov.getTokenListForPhysicalLineAbove(offs);
if (token==null) if (token == null) {
return -1; return -1;
}
// A line containing only Token.NULL is an empty line. // A line containing only Token.NULL is an empty line.
else if (token.getType() == Token.NULL) { else if (token.getType() == Token.NULL) {
int line = c.getLineOfOffset(offs); // Sure to be >0 ?? int line = c.getLineOfOffset(offs); // Sure to be >0 ??
return c.getLineStartOffset(line - 1); return c.getLineStartOffset(line - 1);
} } else {
else {
return token.getListOffset(c, e, 0, x); return token.getListOffset(c, e, 0, x);
} }
@ -804,15 +809,16 @@ public class RSyntaxUtilities implements SwingConstants {
* @param x the X coordinate >= 0 * @param x the X coordinate >= 0
* @return the position >= 0 if the request can be computed, otherwise * @return the position >= 0 if the request can be computed, otherwise
* a value of -1 will be returned. * a value of -1 will be returned.
* @exception BadLocationException if the offset is out of range * @throws BadLocationException if the offset is out of range
*/ */
public static final int getPositionBelow(RSyntaxTextArea c, int offs, public static final int getPositionBelow(RSyntaxTextArea c, int offs,
float x, TabExpander e) throws BadLocationException { float x, TabExpander e) throws BadLocationException {
TokenOrientedView tov = (TokenOrientedView) e; TokenOrientedView tov = (TokenOrientedView) e;
Token token = tov.getTokenListForPhysicalLineBelow(offs); Token token = tov.getTokenListForPhysicalLineBelow(offs);
if (token==null) if (token == null) {
return -1; return -1;
}
// A line containing only Token.NULL is an empty line. // A line containing only Token.NULL is an empty line.
else if (token.getType() == Token.NULL) { else if (token.getType() == Token.NULL) {
@ -821,9 +827,7 @@ public class RSyntaxUtilities implements SwingConstants {
FoldManager fm = c.getFoldManager(); FoldManager fm = c.getFoldManager();
line = fm.getVisibleLineBelow(line); line = fm.getVisibleLineBelow(line);
return c.getLineStartOffset(line); return c.getLineStartOffset(line);
} } else {
else {
return token.getListOffset(c, e, 0, x); return token.getListOffset(c, e, 0, x);
} }
@ -869,9 +873,10 @@ return c.getLineStartOffset(line);
*/ */
public static final Token getTokenAtOffset(Token tokenList, int offset) { public static final Token getTokenAtOffset(Token tokenList, int offset) {
for (Token t = tokenList; t != null && t.isPaintable(); t = t.getNextToken()) { for (Token t = tokenList; t != null && t.isPaintable(); t = t.getNextToken()) {
if (t.containsPosition(offset)) if (t.containsPosition(offset)) {
return t; return t;
} }
}
return null; return null;
} }
@ -901,12 +906,14 @@ return c.getLineStartOffset(line);
int count = s.length(); int count = s.length();
char ch = s.charAt(i); char ch = s.charAt(i);
if (Character.isWhitespace(ch)) { if (Character.isWhitespace(ch)) {
while (i<count && Character.isWhitespace(s.charAt(i++))); while (i < count && Character.isWhitespace(s.charAt(i++))) {
;
} }
else if (Character.isLetterOrDigit(ch)) { } else if (Character.isLetterOrDigit(ch)) {
while (i<count && Character.isLetterOrDigit(s.charAt(i++))); while (i < count && Character.isLetterOrDigit(s.charAt(i++))) {
;
} }
else { } else {
i = 2; i = 2;
} }
offs += i - 1; offs += i - 1;
@ -949,8 +956,7 @@ return c.getLineStartOffset(line);
i--; i--;
} }
offs = lineStart + i; offs = lineStart + i;
} } else if (Character.isLetterOrDigit(ch)) {
else if (Character.isLetterOrDigit(ch)) {
while (i > 0 && Character.isLetterOrDigit(s.charAt(i - 1))) { while (i > 0 && Character.isLetterOrDigit(s.charAt(i - 1))) {
i--; i--;
} }
@ -968,7 +974,7 @@ return c.getLineStartOffset(line);
* Determines the width of the given token list taking tabs * Determines the width of the given token list taking tabs
* into consideration. This is implemented in a 1.1 style coordinate * into consideration. This is implemented in a 1.1 style coordinate
* system where ints are used and 72dpi is assumed.<p> * system where ints are used and 72dpi is assumed.<p>
* * <p>
* This method also assumes that the passed-in token list begins at * This method also assumes that the passed-in token list begins at
* x-pixel <code>0</code> in the view (for tab purposes). * x-pixel <code>0</code> in the view (for tab purposes).
* *
@ -1203,8 +1209,9 @@ return c.getLineStartOffset(line);
// We do it this way as we'd need to do two conditions anyway (first // We do it this way as we'd need to do two conditions anyway (first
// to check that ch<255 so it can index into our table, then whether // to check that ch<255 so it can index into our table, then whether
// that table position has the upper-case mask). // that table position has the upper-case mask).
if (ch>='A' && ch<='Z') if (ch >= 'A' && ch <= 'Z') {
return (char) (ch | 0x20); return (char) (ch | 0x20);
}
return ch; return ch;
} }
@ -1233,15 +1240,16 @@ return c.getLineStartOffset(line);
String osName = System.getProperty("os.name"); String osName = System.getProperty("os.name");
if (osName != null) { // Should always be true. if (osName != null) { // Should always be true.
osName = osName.toLowerCase(); osName = osName.toLowerCase();
if (osName.indexOf("windows") > -1) if (osName.contains("windows")) {
os = OS_WINDOWS; os = OS_WINDOWS;
else if (osName.indexOf("mac os x") > -1) } else if (osName.contains("mac os x")) {
os = OS_MAC_OSX; os = OS_MAC_OSX;
else if (osName.indexOf("linux") > -1) } else if (osName.contains("linux")) {
os = OS_LINUX; os = OS_LINUX;
else } else {
os = OS_OTHER; os = OS_OTHER;
} }
}
return os; return os;
} }
@ -1280,12 +1288,17 @@ return c.getLineStartOffset(line);
sb.append('^'); sb.append('^');
break; break;
case '\\': case '\\':
case '.': case '|': case '.':
case '+': case '-': case '|':
case '+':
case '-':
case '$': case '$':
case '[': case ']': case '[':
case '{': case '}': case ']':
case '(': case ')': case '{':
case '}':
case '(':
case ')':
sb.append('\\').append(ch); sb.append('\\').append(ch);
break; break;
default: default:

Loading…
Cancel
Save