Skip to content

Commit 2264c85

Browse files
committed
fix hidden comments & statements + completion api
1 parent 5b85ff8 commit 2264c85

2 files changed

Lines changed: 59 additions & 14 deletions

File tree

source/dietc/complete.d

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,14 @@ class DietComplete
420420
}
421421
}
422422

423-
void extractD(DietComplete complete, size_t offset, out string code, out size_t codeOffset)
423+
void extractD(DietComplete complete, size_t offset, out string code, out size_t codeOffset, string prefix = null)
424424
{
425-
return complete.parser.root.extractD(offset, code, codeOffset);
425+
return complete.parser.root.extractD(offset, code, codeOffset, prefix);
426426
}
427427

428-
void extractD(AST root, size_t offset, out string code, out size_t codeOffset)
428+
void extractD(AST root, size_t offset, out string code, out size_t codeOffset, string prefix = null)
429429
{
430+
code = prefix;
430431
codeOffset = size_t.max;
431432
class CodeVisitorImpl : ASTVisitor
432433
{

source/dietc/parser.d

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -757,27 +757,29 @@ struct ASTParser
757757
Comment parseComment()
758758
{
759759
auto tok = input.front;
760-
if (input.matchText("//-"))
760+
if (input.matchText("//"))
761761
{
762+
bool hidden = input.front.content.startsWith("-");
762763
string content = parseText(true, true);
764+
if (hidden)
765+
content = content[1 .. $];
766+
tok.type = TokenType.raw;
767+
tok.content = hidden ? "//-" : "//";
763768
tok.range[1] = input.front.range[0];
764-
return new HiddenComment(tok, content);
765-
}
766-
else if (input.matchText("//"))
767-
{
768-
string content = parseText(true, true);
769-
tok.range[1] = input.front.range[0];
770-
return new Comment(tok, content);
769+
return hidden ? new HiddenComment(tok, content) : new Comment(tok, content);
771770
}
772771
return null;
773772
}
774773

775774
DStatement parseDStatement()
776775
{
777776
auto tok = input.front;
778-
if (input.matchText("-"))
777+
if (tok.content.startsWith("-"))
779778
{
780779
string content = parseText(false, false);
780+
content = content[1 .. $];
781+
tok.content = "-";
782+
tok.type = TokenType.raw;
781783
tok.range[1] = input.front.range[0];
782784
return new DStatement(tok, content);
783785
}
@@ -1536,8 +1538,10 @@ unittest
15361538
bar1.tag.assertToken(TokenType.identifier, "div", [5, 5]);
15371539
bar2.tag.assertToken(TokenType.identifier, "div", [18, 18]);
15381540

1539-
assert(cast(TextLine) bar1.contents, "Expected string contents but got " ~ bar1.contents.to!string);
1540-
assert(cast(TextLine) bar2.contents, "Expected string contents but got " ~ bar2.contents.to!string);
1541+
assert(cast(TextLine) bar1.contents,
1542+
"Expected string contents but got " ~ bar1.contents.to!string);
1543+
assert(cast(TextLine) bar2.contents,
1544+
"Expected string contents but got " ~ bar2.contents.to!string);
15411545

15421546
assert((cast(TextLine) bar1.contents)._parts.length == 1);
15431547
assert((cast(TextLine) bar2.contents)._parts.length == 1);
@@ -1585,3 +1589,43 @@ unittest
15851589
assert(content._parts[0].inlineExpr.content == "item.foo");
15861590
assert(content._parts[1].raw == " bar");
15871591
}
1592+
1593+
unittest
1594+
{
1595+
DietInput input;
1596+
input.file = "stdin";
1597+
input.code = `//-foo`;
1598+
1599+
auto parser = new ASTParser;
1600+
parser.input = input.save;
1601+
parser.parseDocument();
1602+
1603+
assert(parser.input.errors.length == 0);
1604+
1605+
assert(parser.root);
1606+
assert(parser.root.children.length == 1);
1607+
auto root = cast(HiddenComment) parser.root.children[0];
1608+
assert(root);
1609+
assertToken(root.token, TokenType.raw, "//-");
1610+
assert(root.content == "foo");
1611+
}
1612+
1613+
unittest
1614+
{
1615+
DietInput input;
1616+
input.file = "stdin";
1617+
input.code = `-foo`;
1618+
1619+
auto parser = new ASTParser;
1620+
parser.input = input.save;
1621+
parser.parseDocument();
1622+
1623+
assert(parser.input.errors.length == 0);
1624+
1625+
assert(parser.root);
1626+
assert(parser.root.children.length == 1);
1627+
auto root = cast(DStatement) parser.root.children[0];
1628+
assert(root);
1629+
assertToken(root.token, TokenType.raw, "-");
1630+
assert(root.content == "foo");
1631+
}

0 commit comments

Comments
 (0)