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

Unsolicited folds in fenced code blocks #1

Closed
taketwo opened this issue Nov 24, 2012 · 11 comments
Closed

Unsolicited folds in fenced code blocks #1

taketwo opened this issue Nov 24, 2012 · 11 comments

Comments

@taketwo
Copy link

taketwo commented Nov 24, 2012

Hi, thanks for the great plugin!
I have noticed that it does not treat fenced code blocks properly though. Consider the following document:

# Minimal C++ program

Here is a minimal C++ program which prints "Hello world!":

```cpp
#include <iostream>
void main()
{
  std::cout << "Hello world!";
}
```

The plugin treats #include directive as a section header and produces two folds:

1 # Minimal C++ program [4 lines]------------------------------------------
6 # include <iostream> [5 lines]-------------------------------------------
@nelstrom
Copy link
Collaborator

Thanks for reporting this. I'll look into it.

@nelstrom
Copy link
Collaborator

Please check out the fenced-code-blocks branch to try out a 1st draft solution. It has unit tests, but it could do with some user testing too. I'm concerned that it might have a negative impact on performance, so please report back if you find it to be in any way laggy.

@taketwo
Copy link
Author

taketwo commented Nov 26, 2012

Thank you for the prompt response and fix! I have tried it with some of my documents and it worked as expected. The documents were not very large, about 200 lines at most, and I did not mention any lags. However, when I created a new document with 1000 lines and more than 50 code blocks, opening it (and folding with zi) took some small yet noticeable amount of time. With 3000 lines my CPU was at 100% for a while. I should mention that the current version from master branch opens the file instantly.
To conclude, for my use-case the fix seems to be sufficient. For someone who works with really large files it will probably bring an unacceptable performance degradation.

@nelstrom
Copy link
Collaborator

I've figured out how to profile the performance of the folding script. Here's the bottleneck; the searchpairpos() function is computationally expensive. In my naive first attempt at a solution for this issue, I was calling searchpairpos() on every line of the file. This patch produces a big performance boost. Testing with a 1500 line .md file, it went from 5 seconds to 1 second.

Still room for improvement.

@nelstrom
Copy link
Collaborator

This patch further improves performance. Testing with the same 1500 line .md file, it's gone from 1.2 seconds to 0.2 seconds. That's more like it!

Instead of using searchpairpos(), I'm simply checking if the current line belongs to the syntax group 'markdownFold'. That introduces a dependency on vim-markdown, but I think that's acceptable.

@taketwo
Copy link
Author

taketwo commented Nov 30, 2012

I think the dependency on vim-markdown is perfectly reasonable. There is a caveat in using the syntax group set by the plugin though. It has an option g:markdown_fenced_languages which allows to do proper syntax highlighting inside fenced blocks. In my case I have let g:markdown_fenced_languages = ['cpp', 'python'], and the simple example which I have posted in the beginning of this thread is not folded properly.

@nelstrom
Copy link
Collaborator

Ok, I think I've covered that now. Try the latest version on the fenced-code-blocks branch.

@taketwo
Copy link
Author

taketwo commented Nov 30, 2012

That solved the issue!

Here is a new one:

# Minimal C++ program

Here is a minimal C++ program which prints "Hello world!":

```cpp
/*
Main function
=============
*/
#include <iostream>
void main()
{
  std::cout << "Hello world!";
}
```

This example is contrived, but you can imagine what will happen in a language like Haskell, where comment lines start with two dashes.

@nelstrom
Copy link
Collaborator

Ok, f5ee87 should fix that.

@taketwo
Copy link
Author

taketwo commented Nov 30, 2012

Works like a charm, thank you for the effort!

@taketwo taketwo closed this as completed Nov 30, 2012
@nelstrom
Copy link
Collaborator

That's in the master branch now. Thanks for your help with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants