Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental HiFi tree diff algorithm for use with quick-fixes and refactoring commands in the IDE #2031

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/org/rascalmpl/library/Prelude.java
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,10 @@

return values.string(chars);
}

public IString indent(IString indentation, IString content, IBool indentFirstLine) {
return content.indent(indentation, indentFirstLine.getValue());

Check warning on line 3052 in src/org/rascalmpl/library/Prelude.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/Prelude.java#L3052

Added line #L3052 was not covered by tests
}

public IValue charAt(IString s, IInteger i) throws IndexOutOfBoundsException
//@doc{charAt -- return the character at position i in string s.}
Expand Down
16 changes: 16 additions & 0 deletions src/org/rascalmpl/library/String.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,19 @@ str substitute(str src, map[loc,str] s) {
order = sort([ k | k <- s ], bool(loc a, loc b) { return a.offset < b.offset; });
return ( src | subst1(it, x, s[x]) | x <- order );
}

@synopsis{Indent a block of text}
@description{
Every line in `content` will be indented using the characters
of `indentation`.
}
@benefits{
* This operation executes in constant time, independent of the size of the content
or the indentation.
* Indent is the identity function if `indentation == ""`
}
@pitfalls{
* This function works fine if `indentation` is not spaces or tabs; but it does not make much sense.
}
@javaClass{org.rascalmpl.library.Prelude}
java str indent(str indentation, str content, bool indentFirstLine=false);
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ void executeDocumentEdit(renamed(loc from, loc to)) {
}

void executeDocumentEdit(changed(loc file, list[TextEdit] edits)) {
str content = readFile(file);

content = executeTextEdits(content, edits);

writeFile(file.top, content);
}

str executeTextEdits(str content, list[TextEdit] edits) {
assert isSorted(edits, less=bool (TextEdit e1, TextEdit e2) {
return e1.range.offset < e2.range.offset;
});

str content = readFile(file);

for (replace(loc range, str repl) <- reverse(edits)) {
assert range.top == file.top;
content = "<content[..range.offset]><repl><content[range.offset+range.length..]>";
}

writeFile(file.top, content);
return content;
}
Loading
Loading