refactoring

This commit is contained in:
Vadim Lopatin 2015-01-21 16:41:15 +03:00
parent a788369925
commit 84f7fdc0f9
4 changed files with 163 additions and 158 deletions

View File

@ -201,8 +201,9 @@
<Folder name="dlangide">
<Folder name="ui">
<File path="src\dlangide\ui\commands.d" />
<File path="src\dlangide\ui\dsourceedit.d" />
<File path="src\dlangide\ui\frame.d" />
<File path="outputpanel.d" />
<File path="src\dlangide\ui\outputpanel.d" />
<File path="src\dlangide\ui\wspanel.d" />
</Folder>
<Folder name="workspace">

View File

@ -0,0 +1,161 @@
module dlangide.ui.dsourceedit;
/// DIDE source file editor
class DSourceEdit : SourceEdit {
this(string ID) {
super(ID);
styleId = null;
backgroundColor = 0xFFFFFF;
setTokenHightlightColor(TokenCategory.Comment, 0x008000); // green
setTokenHightlightColor(TokenCategory.Keyword, 0x0000FF); // blue
setTokenHightlightColor(TokenCategory.String, 0xA31515); // brown
setTokenHightlightColor(TokenCategory.Character, 0xA31515); // brown
setTokenHightlightColor(TokenCategory.Error, 0xFF0000); // red
setTokenHightlightColor(TokenCategory.Comment_Documentation, 0x206000);
//setTokenHightlightColor(TokenCategory.Identifier, 0x206000); // no colors
}
this() {
this("SRCEDIT");
}
protected ProjectSourceFile _projectSourceFile;
@property ProjectSourceFile projectSourceFile() { return _projectSourceFile; }
/// load by filename
override bool load(string fn) {
_projectSourceFile = null;
bool res = super.load(fn);
setHighlighter();
return res;
}
void setHighlighter() {
if (filename.endsWith(".d") || filename.endsWith(".dd") || filename.endsWith(".dh") || filename.endsWith(".ddoc")) {
content.syntaxHighlighter = new SimpleDSyntaxHighlighter(filename);
} else {
content.syntaxHighlighter = null;
}
}
/// load by project item
bool load(ProjectSourceFile f) {
if (!load(f.filename)) {
_projectSourceFile = null;
return false;
}
_projectSourceFile = f;
setHighlighter();
return true;
}
}
class SimpleDSyntaxHighlighter : SyntaxHighlighter {
SourceFile _file;
ArraySourceLines _lines;
Tokenizer _tokenizer;
this (string filename) {
_file = new SourceFile(filename);
_lines = new ArraySourceLines();
_tokenizer = new Tokenizer(_lines);
_tokenizer.errorTolerant = true;
}
TokenPropString[] _props;
/// categorize characters in content by token types
void updateHighlight(dstring[] lines, TokenPropString[] props, int changeStartLine, int changeEndLine) {
Log.d("updateHighlight");
long ms0 = currentTimeMillis();
_props = props;
changeStartLine = 0;
changeEndLine = lines.length;
_lines.init(lines[changeStartLine..$], _file, changeStartLine);
_tokenizer.init(_lines);
int tokenPos = 0;
int tokenLine = 0;
ubyte category = 0;
try {
for (;;) {
Token token = _tokenizer.nextToken();
if (token is null) {
//Log.d("Null token returned");
break;
}
if (token.type == TokenType.EOF) {
//Log.d("EOF token");
break;
}
uint newPos = token.pos - 1;
uint newLine = token.line - 1;
//Log.d("", token.line, ":", token.pos, "\t", tokenLine + 1, ":", tokenPos + 1, "\t", token.toString);
// fill with category
for (int i = tokenLine; i <= newLine; i++) {
int start = i > tokenLine ? 0 : tokenPos;
int end = i < newLine ? lines[i].length : newPos;
for (int j = start; j < end; j++)
_props[i][j] = category;
}
// handle token - convert to category
switch(token.type) {
case TokenType.COMMENT:
category = token.isDocumentationComment ? TokenCategory.Comment_Documentation : TokenCategory.Comment;
break;
case TokenType.KEYWORD:
category = TokenCategory.Keyword;
break;
case TokenType.IDENTIFIER:
category = TokenCategory.Identifier;
break;
case TokenType.STRING:
category = TokenCategory.String;
break;
case TokenType.CHARACTER:
category = TokenCategory.Character;
break;
case TokenType.INTEGER:
category = TokenCategory.Integer;
break;
case TokenType.FLOAT:
category = TokenCategory.Float;
break;
case TokenType.INVALID:
switch (token.invalidTokenType) {
case TokenType.IDENTIFIER:
category = TokenCategory.Error_InvalidIdentifier;
break;
case TokenType.STRING:
category = TokenCategory.Error_InvalidString;
break;
case TokenType.COMMENT:
category = TokenCategory.Error_InvalidComment;
break;
case TokenType.FLOAT:
case TokenType.INTEGER:
category = TokenCategory.Error_InvalidNumber;
break;
default:
category = TokenCategory.Error;
break;
}
break;
default:
category = 0;
break;
}
tokenPos = newPos;
tokenLine= newLine;
}
} catch (Exception e) {
Log.e("exception while trying to parse D source", e);
}
_lines.close();
_props = null;
Log.d("updateHighlight took ", currentTimeMillis() - ms0, "ms");
}
}

View File

@ -26,163 +26,6 @@ import std.conv;
import std.utf;
import std.algorithm;
class SimpleDSyntaxHighlighter : SyntaxHighlighter {
SourceFile _file;
ArraySourceLines _lines;
Tokenizer _tokenizer;
this (string filename) {
_file = new SourceFile(filename);
_lines = new ArraySourceLines();
_tokenizer = new Tokenizer(_lines);
_tokenizer.errorTolerant = true;
}
TokenPropString[] _props;
/// categorize characters in content by token types
void updateHighlight(dstring[] lines, TokenPropString[] props, int changeStartLine, int changeEndLine) {
Log.d("updateHighlight");
long ms0 = currentTimeMillis();
_props = props;
changeStartLine = 0;
changeEndLine = lines.length;
_lines.init(lines[changeStartLine..$], _file, changeStartLine);
_tokenizer.init(_lines);
int tokenPos = 0;
int tokenLine = 0;
ubyte category = 0;
try {
for (;;) {
Token token = _tokenizer.nextToken();
if (token is null) {
//Log.d("Null token returned");
break;
}
if (token.type == TokenType.EOF) {
//Log.d("EOF token");
break;
}
uint newPos = token.pos - 1;
uint newLine = token.line - 1;
//Log.d("", token.line, ":", token.pos, "\t", tokenLine + 1, ":", tokenPos + 1, "\t", token.toString);
// fill with category
for (int i = tokenLine; i <= newLine; i++) {
int start = i > tokenLine ? 0 : tokenPos;
int end = i < newLine ? lines[i].length : newPos;
for (int j = start; j < end; j++)
_props[i][j] = category;
}
// handle token - convert to category
switch(token.type) {
case TokenType.COMMENT:
category = token.isDocumentationComment ? TokenCategory.Comment_Documentation : TokenCategory.Comment;
break;
case TokenType.KEYWORD:
category = TokenCategory.Keyword;
break;
case TokenType.IDENTIFIER:
category = TokenCategory.Identifier;
break;
case TokenType.STRING:
category = TokenCategory.String;
break;
case TokenType.CHARACTER:
category = TokenCategory.Character;
break;
case TokenType.INTEGER:
category = TokenCategory.Integer;
break;
case TokenType.FLOAT:
category = TokenCategory.Float;
break;
case TokenType.INVALID:
switch (token.invalidTokenType) {
case TokenType.IDENTIFIER:
category = TokenCategory.Error_InvalidIdentifier;
break;
case TokenType.STRING:
category = TokenCategory.Error_InvalidString;
break;
case TokenType.COMMENT:
category = TokenCategory.Error_InvalidComment;
break;
case TokenType.FLOAT:
case TokenType.INTEGER:
category = TokenCategory.Error_InvalidNumber;
break;
default:
category = TokenCategory.Error;
break;
}
break;
default:
category = 0;
break;
}
tokenPos = newPos;
tokenLine= newLine;
}
} catch (Exception e) {
Log.e("exception while trying to parse D source", e);
}
_lines.close();
_props = null;
Log.d("updateHighlight took ", currentTimeMillis() - ms0, "ms");
}
}
/// DIDE source file editor
class DSourceEdit : SourceEdit {
this(string ID) {
super(ID);
styleId = null;
backgroundColor = 0xFFFFFF;
setTokenHightlightColor(TokenCategory.Comment, 0x008000); // green
setTokenHightlightColor(TokenCategory.Keyword, 0x0000FF); // blue
setTokenHightlightColor(TokenCategory.String, 0xA31515); // brown
setTokenHightlightColor(TokenCategory.Character, 0xA31515); // brown
setTokenHightlightColor(TokenCategory.Error, 0xFF0000); // red
setTokenHightlightColor(TokenCategory.Comment_Documentation, 0x206000);
//setTokenHightlightColor(TokenCategory.Identifier, 0x206000); // no colors
}
this() {
this("SRCEDIT");
}
protected ProjectSourceFile _projectSourceFile;
@property ProjectSourceFile projectSourceFile() { return _projectSourceFile; }
/// load by filename
override bool load(string fn) {
_projectSourceFile = null;
bool res = super.load(fn);
setHighlighter();
return res;
}
void setHighlighter() {
if (filename.endsWith(".d") || filename.endsWith(".dd") || filename.endsWith(".dh") || filename.endsWith(".ddoc")) {
content.syntaxHighlighter = new SimpleDSyntaxHighlighter(filename);
} else {
content.syntaxHighlighter = null;
}
}
/// load by project item
bool load(ProjectSourceFile f) {
if (!load(f.filename)) {
_projectSourceFile = null;
return false;
}
_projectSourceFile = f;
setHighlighter();
return true;
}
}
/// DIDE app frame
class IDEFrame : AppFrame {