Skip to content

Commit

Permalink
fix hidden comments & statements + completion api
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Jul 9, 2019
1 parent 5b85ff8 commit 2264c85
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
7 changes: 4 additions & 3 deletions source/dietc/complete.d
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,14 @@ class DietComplete
}
}

void extractD(DietComplete complete, size_t offset, out string code, out size_t codeOffset)
void extractD(DietComplete complete, size_t offset, out string code, out size_t codeOffset, string prefix = null)
{
return complete.parser.root.extractD(offset, code, codeOffset);
return complete.parser.root.extractD(offset, code, codeOffset, prefix);
}

void extractD(AST root, size_t offset, out string code, out size_t codeOffset)
void extractD(AST root, size_t offset, out string code, out size_t codeOffset, string prefix = null)
{
code = prefix;
codeOffset = size_t.max;
class CodeVisitorImpl : ASTVisitor
{
Expand Down
66 changes: 55 additions & 11 deletions source/dietc/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -757,27 +757,29 @@ struct ASTParser
Comment parseComment()
{
auto tok = input.front;
if (input.matchText("//-"))
if (input.matchText("//"))
{
bool hidden = input.front.content.startsWith("-");
string content = parseText(true, true);
if (hidden)
content = content[1 .. $];
tok.type = TokenType.raw;
tok.content = hidden ? "//-" : "//";
tok.range[1] = input.front.range[0];
return new HiddenComment(tok, content);
}
else if (input.matchText("//"))
{
string content = parseText(true, true);
tok.range[1] = input.front.range[0];
return new Comment(tok, content);
return hidden ? new HiddenComment(tok, content) : new Comment(tok, content);
}
return null;
}

DStatement parseDStatement()
{
auto tok = input.front;
if (input.matchText("-"))
if (tok.content.startsWith("-"))
{
string content = parseText(false, false);
content = content[1 .. $];
tok.content = "-";
tok.type = TokenType.raw;
tok.range[1] = input.front.range[0];
return new DStatement(tok, content);
}
Expand Down Expand Up @@ -1536,8 +1538,10 @@ unittest
bar1.tag.assertToken(TokenType.identifier, "div", [5, 5]);
bar2.tag.assertToken(TokenType.identifier, "div", [18, 18]);

assert(cast(TextLine) bar1.contents, "Expected string contents but got " ~ bar1.contents.to!string);
assert(cast(TextLine) bar2.contents, "Expected string contents but got " ~ bar2.contents.to!string);
assert(cast(TextLine) bar1.contents,
"Expected string contents but got " ~ bar1.contents.to!string);
assert(cast(TextLine) bar2.contents,
"Expected string contents but got " ~ bar2.contents.to!string);

assert((cast(TextLine) bar1.contents)._parts.length == 1);
assert((cast(TextLine) bar2.contents)._parts.length == 1);
Expand Down Expand Up @@ -1585,3 +1589,43 @@ unittest
assert(content._parts[0].inlineExpr.content == "item.foo");
assert(content._parts[1].raw == " bar");
}

unittest
{
DietInput input;
input.file = "stdin";
input.code = `//-foo`;

auto parser = new ASTParser;
parser.input = input.save;
parser.parseDocument();

assert(parser.input.errors.length == 0);

assert(parser.root);
assert(parser.root.children.length == 1);
auto root = cast(HiddenComment) parser.root.children[0];
assert(root);
assertToken(root.token, TokenType.raw, "//-");
assert(root.content == "foo");
}

unittest
{
DietInput input;
input.file = "stdin";
input.code = `-foo`;

auto parser = new ASTParser;
parser.input = input.save;
parser.parseDocument();

assert(parser.input.errors.length == 0);

assert(parser.root);
assert(parser.root.children.length == 1);
auto root = cast(DStatement) parser.root.children[0];
assert(root);
assertToken(root.token, TokenType.raw, "-");
assert(root.content == "foo");
}

0 comments on commit 2264c85

Please sign in to comment.