This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Implement format on type
ClosedPublic

Authored by krasimir on Feb 15 2017, 8:00 AM.

Details

Summary

This patch adds onTypeFormatting to clangd.

The trigger character is '}' and it works by scanning for the matching '{' and formatting the range in-between.

There are problems with ';' as a trigger character, the cursor position is before the |:

int main() {
  int i;|
}

becomes:

int main() {  int i;| }

which is not likely what the user intended.

Also formatting at semicolon in a non-properly closed scope puts the following tokens in the same unwrapped line, which doesn't reformat nicely.

Event Timeline

krasimir created this revision.Feb 15 2017, 8:00 AM
krasimir updated this revision to Diff 88540.Feb 15 2017, 8:01 AM
  • removed newline
krasimir edited the summary of this revision. (Show Details)Feb 15 2017, 8:06 AM
krasimir added a reviewer: bkramer.
krasimir added a subscriber: cfe-commits.
bkramer added inline comments.Feb 15 2017, 10:17 AM
clangd/ProtocolHandlers.cpp
117

This should be a std::string in trunk, StringRef gives you a use after free.

119

nit: use the character overload of this function (Code.find_last_of('{'...)

krasimir updated this revision to Diff 88703.Feb 16 2017, 2:08 AM
krasimir edited the summary of this revision. (Show Details)
  • Address review comments
krasimir marked 2 inline comments as done.Feb 16 2017, 2:09 AM
This revision is now accepted and ready to land.Feb 16 2017, 2:26 AM
This revision was automatically updated to reflect the committed changes.