📄 codedocument.java
字号:
Element element = this.getParagraphElement(offs);
String elementText = "";
try
{
// this gets our chuck of current text for the element
// we're on
elementText = this.getText(element.getStartOffset(), element
.getEndOffset()
- element.getStartOffset());
}
catch (Exception ex)
{
System.err.println("no text");
}
int strLen = elementText.length();
if (strLen == 0)
{
return;
}
int i = 0;
if (element.getStartOffset() > 0)
{
// translates backward if neccessary
offs = offs - element.getStartOffset();
}
mode = TEXT_MODE;
if ((offs >= 0) && (offs <= strLen - 1))
{
i = offs;
while (i > 0)
{
// the while loop walks back until we hit a delimiter
char charAt = elementText.charAt(i);
if ((charAt == ' ') | (i == 0) | (charAt == '(')
| (charAt == ')') | (charAt == '{') | (charAt == '}') /* | */)
{ // if i == 0 then we're at the begininng
if (i != 0)
{
i++;
}
mode = NUMBER_MODE;
break;
}
else if (!(charAt >= '0' & charAt <= '9' | charAt == '.'
| charAt == '+' | charAt == '-' | charAt == '/'
| charAt == '*' | charAt == '%' | charAt == '='))
{
mode = TEXT_MODE;
break;
}
i--;
}
}
}
private void checkForComment()
{
int offs = this.currentPos;
Element element = this.getParagraphElement(offs);
String elementText = "";
try
{
// this gets our chuck of current text for the element
// we're on
elementText = this.getText(element.getStartOffset(), element
.getEndOffset()
- element.getStartOffset());
}
catch (Exception ex)
{
System.err.println("no text");
}
int strLen = elementText.length();
if (strLen == 0)
{
return;
}
int i = 0;
if (element.getStartOffset() > 0)
{
// translates backward if neccessary
offs = offs - element.getStartOffset();
}
if ((offs >= 1) && (offs <= strLen - 1))
{
i = offs;
char commentStartChar1 = elementText.charAt(i - 1);
char commentStartChar2 = elementText.charAt(i);
if (commentStartChar1 == '/' && commentStartChar2 == '*')
{
mode = COMMENT_MODE;
this.insertCommentString("/*", currentPos - 1);
}
else if (commentStartChar1 == '*' && commentStartChar2 == '/')
{
mode = TEXT_MODE;
this.insertCommentString("*/", currentPos - 1);
}
}
}
private void processChar(String str)
{
char strChar = str.charAt(0);
if (mode != COMMENT_MODE)
{
mode = TEXT_MODE;
}
switch (strChar)
{
case ('{'):
case ('}'):
case (' '):
case ('\n'):
case ('('):
case (')'):
case (';'):
case ('.'):
{
checkForKeyword();
if (mode == STRING_MODE && strChar == '\n')
{
mode = TEXT_MODE;
}
}
break;
case ('"'):
{
insertTextString(str, currentPos);
this.checkForString();
}
break;
case ('0'):
case ('1'):
case ('2'):
case ('3'):
case ('4'):
case ('5'):
case ('6'):
case ('7'):
case ('8'):
case ('9'):
{
checkForNumber();
}
break;
case ('*'):
case ('/'):
{
checkForComment();
}
break;
}
if (mode == TEXT_MODE)
{
this.checkForString();
}
if (mode == STRING_MODE)
{
insertTextString(str, this.currentPos);
}
else if (mode == NUMBER_MODE)
{
insertNumberString(str, this.currentPos);
}
else if (mode == COMMENT_MODE)
{
insertCommentString(str, this.currentPos);
}
}
private void processChar(char strChar)
{
char[] chrstr = new char[1];
chrstr[0] = strChar;
String str = new String(chrstr);
processChar(str);
}
// ////////////////////////////////////////////////////////////////////////
// Brace matching specific methods
// ////////////////////////////////////////////////////////////////////////
private void resetBracePosition()
{
if (braceIndex1 == -1 && braceIndex2 == -1)
{
return;
}
applyBraceHiglight(false, braceIndex1, braceIndex2);
braceIndex1 = -1;
braceIndex2 = -1;
}
private void applyBraceHiglight(boolean apply, int index1, int index2)
{
Style style = apply ? braceStyle : normalStyle;
if (index1 != -1)
{
setCharacterAttributes(index1, 1, style, !apply);
}
if (index2 != -1)
{
setCharacterAttributes(index2, 1, style, !apply);
}
}
private void updateBraces(int offset)
{
try
{
int length = getLength();
String text = getText(0, length);
char charAtOffset = 0;
char charBeforeOffset = 0;
if (offset > 0)
{
charBeforeOffset = text.charAt(offset - 1);
}
if (offset < length)
{
charAtOffset = text.charAt(offset);
}
int matchOffset = -1;
if (isBrace(charAtOffset))
{
matchOffset = getMatchingBraceOffset(offset, charAtOffset, text);
}
else if (isBrace(charBeforeOffset))
{
offset--;
matchOffset = getMatchingBraceOffset(offset, charBeforeOffset,
text);
}
if (matchOffset != -1)
{
braceIndex1 = offset;
braceIndex2 = matchOffset;
applyBraceHiglight(true, offset, matchOffset);
}
}
catch (BadLocationException e)
{
throw new Error(e);
}
}
private int getMatchingBraceOffset(int offset, char brace, String text)
{
int thisBraceCount = 0;
int matchingBraceCount = 0;
char braceMatch = getMatchingBrace(brace);
char[] chars = text.toCharArray();
if (isOpenBrace(brace))
{
for (int i = offset; i < chars.length; i++)
{
if (chars[i] == brace)
{
thisBraceCount++;
}
else if (chars[i] == braceMatch)
{
matchingBraceCount++;
}
if (thisBraceCount == matchingBraceCount)
{
return i;
}
}
}
else
{
for (int i = offset; i >= 0; i--)
{
if (chars[i] == brace)
{
thisBraceCount++;
}
else if (chars[i] == braceMatch)
{
matchingBraceCount++;
}
if (thisBraceCount == matchingBraceCount)
{
return i;
}
}
}
return -1;
}
private char getMatchingBrace(char brace)
{
switch (brace)
{
case '(':
return ')';
case ')':
return '(';
case '[':
return ']';
case ']':
return '[';
case '{':
return '}';
case '}':
return '{';
default:
return 0;
}
}
private boolean isOpenBrace(char brace)
{
switch (brace)
{
case '(':
case '[':
case '{':
return true;
}
return false;
}
private boolean isBrace(char charAt)
{
for (int i = 0; i < BRACES.length; i++)
{
if (charAt == BRACES[i])
{
return true;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -