Skip to content

Commit

Permalink
fixed a lot of D completion edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Jul 9, 2019
1 parent 6e63db8 commit 5b85ff8
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"name": "diet-complete",
"description": "Error correcting diet parser with integrated auto-completion and neat AST tools",
"license": "MIT",
"copyright": "Copyright © 2018 webfreak"
"copyright": "Copyright © 2018-2019 webfreak"
}
21 changes: 9 additions & 12 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ void main(string[] args)
DietInput input;
input.file = "stdin";
input.code = q{doctype html
html(lang="de")
head
title Hello World
body
- foreach (index; 0 .. 10)
ul= i};
html

};

auto parser = new ASTParser;
parser.input = input.save;
Expand Down Expand Up @@ -49,11 +46,11 @@ html(lang="de")
writeln();
}

testComplete(24);
testComplete(19);

string code;
size_t offset;
complete.extractD(98, code, offset);
writeln("D:\n", code);
writeln(offset == size_t.max ? "not found" : offset.to!string);
// string code;
// size_t offset;
// complete.extractD(98, code, offset);
// writeln("D:\n", code);
// writeln(offset == size_t.max ? "not found" : offset.to!string);
}
78 changes: 77 additions & 1 deletion source/dietc/complete.d
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,16 @@ void extractD(AST root, size_t offset, out string code, out size_t codeOffset)
static foreach (T; AliasSeq!(Expression, Assignment, RawAssignment))
override void visit(T expr) in (expr !is null)
{
static if (is(T == Expression))
enum typeOffset = 0;
else static if (is(T == Assignment))
enum typeOffset = 1; // offset the equal of tag=
else static if (is(T == RawAssignment))
enum typeOffset = 2; // offset the bang-equal of tag!=

code ~= "__diet_value(";
if (offset.withinRange(expr.token.range))
codeOffset = code.length + (offset - expr.token.range[0] - expr.token.content.length);
codeOffset = code.length + (offset - typeOffset - expr.token.range[0]);
code ~= expr.content;
code ~= ");";
}
Expand Down Expand Up @@ -526,3 +533,72 @@ unittest
assert(testComplete("", 0).canFind!(a => a.text == "textarea"));
assert(testComplete("div\n\t", 5).canFind!(a => a.text == "textarea"));
}

unittest
{
import std.conv;

DietInput input;
input.file = "stdin";
input.code = `foo
- int item = 3;
p #{item.foobar} bar
a(attr=foo.bar)= foo.bar
`;
auto c = new DietComplete(input, cast(FileProvider)(name) {
assert(false, "Can't import " ~ name ~ " in test");
});

assert(c.completeAt(23).canFind!(a => a.text == "pre"));
assert(c.completeAt(39).length == 0);
assert(c.completeAt(38).length == 0);
assert(c.completeAt(24).length == 0);
assert(c.completeAt(25).length == 0);

void checkDBounds(size_t start, size_t end)
{
assert(c.completeAt(start - 1) !is Completion.completeD);
assert(c.completeAt(start) is Completion.completeD);
assert(c.completeAt(end) is Completion.completeD);
assert(c.completeAt(end + 1) !is Completion.completeD);
}

checkDBounds(6, 20);
checkDBounds(26, 37);
checkDBounds(51, 58);
checkDBounds(60, 68);

string code;
size_t offset;

c.extractD(28, code, offset);
assert(code == `void __diet_document() {{ int item = 3;{__diet_value(item.foobar);}{{__diet_value(foo.bar);}__diet_value( foo.bar);}}}`);
assert(offset == 55);

c.extractD(37, code, offset);
assert(offset == 64);

c.extractD(38, code, offset);
assert(offset == size_t.max);

c.extractD(25, code, offset);
assert(offset == size_t.max);

c.extractD(7, code, offset);
assert(offset == 26);

c.extractD(20, code, offset);
assert(offset == 39);

c.extractD(51, code, offset);
assert(offset == 82);

c.extractD(58, code, offset);
assert(offset == 89);

c.extractD(61, code, offset);
assert(offset == 106);

c.extractD(68, code, offset);
assert(offset == 113);
}
31 changes: 28 additions & 3 deletions source/dietc/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ struct DietInput
Token tok = front;
auto ret = this.match(type, match);
if (!ret)
errors.expect(this, at, (match.length ? "'" ~ match ~ "'"
: type.to!string) ~ ", but got " ~ tok.to!string, srcfile, srcline);
errors.expect(this, at, (match.length
? "'" ~ match ~ "'" : type.to!string) ~ ", but got " ~ tok.to!string, srcfile, srcline);
return ret;
}

Expand Down Expand Up @@ -400,7 +400,9 @@ struct DietInput
return ret;
}
else
return [Token(TokenType.whitespace, code[start .. index], [start, index])];
return [
Token(TokenType.whitespace, code[start .. index], [start, index])
];
}
else if (c.tagIdentifierValidator)
{
Expand Down Expand Up @@ -489,3 +491,26 @@ string indent(string s, string indentation = "\t")
.map!(a => a.strip.length ? indentation ~ a : a)
.join("");
}

unittest
{
import std.array : array;

DietInput input;
input.file = "stdin";
input.code = q{doctype html
html

};

auto tokens = input.array;
assert(input.errors.length == 0);

with (TokenType)
assert(tokens == [
Token(identifier, "doctype", [0, 7]), Token(whitespace, " ", [7, 8]),
Token(identifier, "html", [8, 12]), Token(newline, "\n", [12, 13]),
Token(identifier, "html", [13, 17]), Token(newline, "\n", [17, 18]),
Token(newline, "\n", [18, 19])
]);
}
Loading

0 comments on commit 5b85ff8

Please sign in to comment.