Skip to content

Commit

Permalink
Leave closing brace exposed, only fold comments >2 lines
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
akbyrd committed May 4, 2019
1 parent 520c687 commit fb6fe10
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 80 deletions.
33 changes: 22 additions & 11 deletions binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ clang_Cursor_isKeywordWithCompound(CXCursor cursor)
void
AppendFoldingRange(Env env, Array ranges, unsigned lineStart, unsigned lineEnd)
{
if (lineEnd != lineStart)
if (lineEnd > lineStart)
{
Object range = Object::New(env);
range.Set(String::New(env, "start"), lineStart - 1);
Expand Down Expand Up @@ -223,15 +223,26 @@ ParseFile(const CallbackInfo& info)
case '}':
{
if (tokenStack.empty()) break;
CXToken* startToken = tokenStack.top(); tokenStack.pop();
CXToken* endToken = &token;
if (!startToken) break;
AppendFoldingRange(env, ranges, unit, *startToken, *endToken);
CXToken* tokenStart = tokenStack.top(); tokenStack.pop();
CXToken* tokenEnd = &token;
if (!tokenStart) break;

CXSourceRange tokenRangeStart = clang_getTokenExtent(unit, *tokenStart);
CXSourceRange tokenRangeEnd = clang_getTokenExtent(unit, *tokenEnd);

CXSourceLocation srcStart = clang_getRangeStart(tokenRangeStart);
CXSourceLocation srcEnd = clang_getRangeEnd (tokenRangeEnd);

unsigned lineStart, lineEnd;
clang_getFileLocation(srcStart, nullptr, &lineStart, nullptr, nullptr);
clang_getFileLocation(srcEnd, nullptr, &lineEnd, nullptr, nullptr);

AppendFoldingRange(env, ranges, lineStart, lineEnd - 1);
prevToken = nullptr;

// DEBUG
std::string tokenStartStr = clang_getCStr(clang_getTokenSpelling(unit, *startToken));
std::string tokenEndStr = clang_getCStr(clang_getTokenSpelling(unit, *endToken));
std::string tokenStartStr = clang_getCStr(clang_getTokenSpelling(unit, *tokenStart));
std::string tokenEndStr = clang_getCStr(clang_getTokenSpelling(unit, *tokenEnd));
log("pop & range: ", tokenStartStr, " - ", tokenEndStr);
break;
}
Expand All @@ -253,11 +264,10 @@ ParseFile(const CallbackInfo& info)
{
if (tokenKind == CXToken_Comment)
{
CXString tokenStr = clang_getTokenSpelling(unit, token);
const char* tokenCStr = clang_getCString(tokenStr);
CXSourceRange srcRange = clang_getTokenExtent(unit, token);
std::string tokenStr = clang_getCStr(clang_getTokenSpelling(unit, token));
CXSourceRange srcRange = clang_getTokenExtent(unit, token);

bool isBlockComment = strncmp("/*", tokenCStr, 2) == 0;
bool isBlockComment = strncmp("/*", tokenStr.c_str(), 2) == 0;
if (isBlockComment)
{
End(env, ranges);
Expand Down Expand Up @@ -316,6 +326,7 @@ ParseFile(const CallbackInfo& info)
if (inComment)
{
inComment = false;
if (commentEnd - commentStart + 1 < 3) return;
AppendFoldingRange(env, ranges, commentStart, commentEnd);
}
};
Expand Down
63 changes: 0 additions & 63 deletions notes.txt

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"publisher": "abg",
"displayName": "C++ Code Folding",
"description": "Enable syntax-based code folding for C and C++",
"version": "0.0.2",
"repository": "https://github.com/akbyrd/vsc-cpp-folding",
"version": "0.0.3",
"engines": {
"vscode": "^1.30.0"
},
Expand Down
48 changes: 48 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Development Requirements
------------------------
- node.js
- git
- python2?



Development Notes
-----------------
`node-gyp rebuild` needs to be run to compile the native code. This isn't directly available at the command line by default. So either `npm install` to indirectly trigger it to or `npm install -g node-gyp` to install it for direct use.\
https://nodejs.org/dist/latest/docs/api/addons.html#addons_building
https://github.com/nodejs/node-addon-api

I haven't taken the time to figure out how to debug native extensions directly.\
https://medium.com/@atulanand94/debugging-nodejs-c-addons-using-vs-code-27e9940fc3ad

Publishing an extension:
- npm install -g vsce
- vsce package
- code --install-extension path/to/ext.vsix

https://code.visualstudio.com/api/working-with-extensions/publishing-extension



Current State
-------------
Folding is in good working condition. Folding is determined entirely from tokens rather than from the AST. LibClang gives up building the AST when an unknown symbol is encountered (the symbol and its lexical children will be skipped and the parent scope will appear as a CompoundStmt). I haven't
tested LibTooling to see what it does because the current solution appears to be very nearly as good as using the AST anyway.

Blocks are folded using curly braces, block comments, and preprocessor blocks (#if and friends). Care is taken to fold the braces up onto the preceding line they are associated with (e.g. the function declaration, for keyword, etc). With closing braces it feels better to leave the brace exposed. This means most things fold into two lines: one with keyword/declaration and a trailing '...' and one with the closing brace '}'. I couldn't find compelling enough cases to justify folding things like function and template parameters (when spread across multiple lines). There might be room to do a token-AST hybrid approach (when the AST is actually available) where function calls that span multiple lines are folded, but that would introduce quite a lot of complexity and it just doesn't feel worth it.

Currently #defines do not fold. This should be reasonably straight forward, I just haven't gotten around to it.



To Do
-----
- [ ] Get LibTooling working and evaluate its capabilities
- [ ] Set folding range kind
- [ ] Consider folding #defines
- [ ] Ensure errors propagate somewhere visible
- [ ] Reduce the final vsix size
- [ ] Avoid copying file contents into a std::string (get the underlying v8 string)
- [ ] Avoid copying errors to an intermediate buffer
- [ ] Test performance
- [ ] Consider avoiding re-parsing when files have not changed
9 changes: 6 additions & 3 deletions src/test/Test Comments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
* Should fold
*/

// Single comment, should not fold

// Sequence Comment
// Should fold
// When 3 or more lines

// Single comment, should not fold

// Gap between comments, should not fold
// Gap between
// Folding comments
// Should fold separately

/* Different comment types */
// Should not fold
Expand Down
4 changes: 2 additions & 2 deletions src/test/Test Lambdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ void Foo2()
{
// Multi-line lambda decl, should fold
auto lambda1 = []() {
// ...
};

// NOTE: Won't fold with the token method
// Mulit-line lambda call, should fold
// Mulit-line lambda call, should not fold
lambda1(
);
}
14 changes: 14 additions & 0 deletions src/test/Test Preprocessor Branches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@
{
}
#endif


// TODO: Find something sane to do here
void Foo3()
{
if (false)
{

#if false
}
#else
}
#endif
}

0 comments on commit fb6fe10

Please sign in to comment.