Skip to content

Commit

Permalink
Merge pull request imagej#20 from imagej/update-rsyntaxtextarea
Browse files Browse the repository at this point in the history
Update RSyntaxTextArea
  • Loading branch information
dscho committed Jun 26, 2014
2 parents 631c24a + fd36ee1 commit b7332c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<dependency>
<groupId>com.fifesoft</groupId>
<artifactId>rsyntaxtextarea</artifactId>
<version>2.0.4.1</version>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.miglayout</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import javax.swing.text.Segment;

import org.fife.ui.rsyntaxtextarea.AbstractJFlexCTokenMaker;
import org.fife.ui.rsyntaxtextarea.DefaultToken;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenImpl;

/**
* TODO
Expand Down Expand Up @@ -2471,7 +2471,7 @@ public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
return yylex();
} catch (IOException ioe) {
ioe.printStackTrace();
return new DefaultToken();
return new TokenImpl();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import javax.swing.text.Segment;

import org.fife.ui.rsyntaxtextarea.AbstractJFlexTokenMaker;
import org.fife.ui.rsyntaxtextarea.DefaultToken;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenImpl;

/**
* Scanner for the Matlab programming language.<p>
Expand Down Expand Up @@ -570,7 +570,7 @@ public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
return yylex();
} catch (IOException ioe) {
ioe.printStackTrace();
return new DefaultToken();
return new TokenImpl();
}

}
Expand Down
51 changes: 29 additions & 22 deletions src/main/java/net/imagej/ui/swing/script/TokenFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenTypes;

/**
* TODO
Expand All @@ -57,36 +58,41 @@ public TokenFunctions(RSyntaxTextArea textArea) {
}

public static boolean tokenEquals(Token token, char[] text) {
if (token.type != token.RESERVED_WORD ||
token.textCount != text.length)
if (token.getType() != TokenTypes.RESERVED_WORD ||
token.length() != text.length)
return false;
final char[] tokenText = token.getTextArray();
if (tokenText == null) return false;
final int textOffset = token.getTextOffset();
for (int i = 0; i < text.length; i++)
if (token.text[token.textOffset + i] != text[i])
if (tokenText[textOffset + i] != text[i])
return false;
return true;
}

public static boolean isIdentifier(Token token) {
if (token.type != token.IDENTIFIER)
if (token.getType() != TokenTypes.IDENTIFIER)
return false;
if (!Character.isJavaIdentifierStart(token.text[token.textOffset]))
final char[] tokenText = token.getTextArray();
final int textOffset = token.getTextOffset();
if (tokenText == null || !Character.isJavaIdentifierStart(tokenText[textOffset]))
return false;
for (int i = 1; i < token.textCount; i++)
if (!Character.isJavaIdentifierPart(token.text[token.textOffset + i]))
for (int i = 1; i < tokenText.length; i++)
if (!Character.isJavaIdentifierPart(tokenText[textOffset + i]))
return false;
return true;
}

public static String getText(Token token) {
if (token.text == null)
final char[] tokenText = token.getTextArray();
if (tokenText == null)
return "";
return new String(token.text,
token.textOffset, token.textCount);
return new String(tokenText, token.getTextOffset(), token.length());
}

public void replaceToken(Token token, String text) {
textArea.replaceRange(text, token.textOffset,
token.textOffset + token.textCount);
textArea.replaceRange(text, token.getOffset(),
token.getEndOffset());
}

class TokenIterator implements Iterator<Token> {
Expand Down Expand Up @@ -130,8 +136,9 @@ public Iterator<Token> iterator() {
}

public static boolean isDot(Token token) {
return token.type == token.IDENTIFIER && token.textCount == 1
&& token.text[token.textOffset] == '.';
if (token.getType() != TokenTypes.IDENTIFIER) return false;
final char[] tokenText = token.getTextArray();
return tokenText != null && token.length() == 1 && tokenText[token.getTextOffset()] == '.';
}

/* The following methods are Java-specific */
Expand Down Expand Up @@ -192,7 +199,7 @@ public String toString() {

Token skipNonCode(TokenIterator iter, Token current) {
for (;;) {
switch (current.type) {
switch (current.getType()) {
case Token.COMMENT_DOCUMENTATION:
case Token.COMMENT_EOL:
case Token.COMMENT_MULTILINE:
Expand All @@ -210,9 +217,9 @@ Token skipNonCode(TokenIterator iter, Token current) {
int skipToEOL(TokenIterator iter, Token current) {
int end = textArea.getDocument().getLength();
for (;;) {
if (current.type == current.NULL || !iter.hasNext())
if (current.getType() == TokenTypes.NULL || !iter.hasNext())
return end;
end = current.offset + current.textCount;
end = current.getEndOffset();
current = iter.next();
}
}
Expand All @@ -224,22 +231,22 @@ public List<Import> getImports() {
TokenIterator iter = new TokenIterator();
while (iter.hasNext()) {
Token token = iter.next();
int offset = token.offset;
int offset = token.getOffset();
token = skipNonCode(iter, token);
if (tokenEquals(token, importChars)) {
do {
if (!iter.hasNext())
return result;
token = iter.next();
} while (!isIdentifier(token));
int start = token.offset, end = start;
int start = token.getOffset(), end = start;
do {
if (!iter.hasNext())
return result;
token = iter.next();
if (isDot(token) && iter.hasNext())
token = iter.next();
end = token.offset + token.textCount;
end = token.getEndOffset();
} while (isIdentifier(token));
String identifier = getText(start, end);
if (identifier.endsWith(";"))
Expand Down Expand Up @@ -302,7 +309,7 @@ public void addImport(String className) {
boolean insertLF = false;
while (iter.hasNext()) {
Token token = iter.next();
if (token.type != token.RESERVED_WORD) {
if (token.getType() != TokenTypes.RESERVED_WORD) {
insertLF = false;
continue;
}
Expand All @@ -311,7 +318,7 @@ public void addImport(String className) {
insertLF = true;
}
else {
offset = token.offset;
offset = token.getOffset();
break;
}
}
Expand Down

0 comments on commit b7332c3

Please sign in to comment.