Open DanielT opened 1 year ago
Use the EditorConfig plugin.
As far as I can tell, the EditorConfig plugin allows me to set .editorconfig files for certain paths, but it does not enable automatic detection. So, while this is an improvement for projects already on my machine, it doesn't solve the issue automatically for every file going forward, the way automatic detection would. In short: it's a band aid, but not a solution.
EditorConfig is a reasonable solution, long accepted in the industry. Visual Studio's text editor knows how to use it; probably VSCode does as well...
However, what exactly is your criteria for determination when opening an existing file? Don't just say "however VSCode does it" -- that would be a cop out.
Examples to get you started:
If a file being opened contains one or more tab characters anywhere within it, assume tab characters are used for indenting...now how do I know how much horizontal space should be given to a tab...
If a file being opened contains no tab characters, assume spaces are used for indenting...now how to determine how many spaces are used (sure this is easy if all lines analyzed are extremely consistent in using e.g. 3 space for each level of indentation, but are all files always going to be this consistent?
What should be done in the mixed tab and space condition, where some lines start off with a tab character and some lines have leading spaces?
As I'm attempting to hint, this is not a trivial problem to solve (i.e., EditorConfig exists for a good reason). But, if you have some great ideas about it, let's hear them.
I don't know the vscode implementation, but it doesn't seem that difficult to come up with a rough algorithm:
1) get the whitespace at the start of all lines; if a solid majority (say 90%) is spaces, then the indentation should be spaces; on the other hand if the majority (e.g. 90%) is tabs, then indent with tabs. If it is undecided, then the people editing were confused and the existing global setting should be used. 2) For the depth, if using spaces, bin the indentation values and find out if the majority of depths are divisible by some integer, and if that exists then use that integer as the indent depth. if there is no clear majority, or tabs are selected then fall back to the setting. 3) Finally, for performance, maybe don't look at the entirety of huge files and limit the analysis to the first (x) MB of input. If someone has a huge file that changes the indent style half way through, then that's their problem.
That's actually pretty good. Now I know you're intelligent. :-)
For efficiency, how about looking at just the very first usage of indentation? It will also give the user a quick way to change what Npp should apply for this file.
Looking at only the first usage of indentation seems like it would not be enough to avoid strange edge cases. For example, what if a file start with a comment:
/** description of my file / class / function / whatever
* Lots of insightful text here ...
*/
Then you would detect the indentation as being one space, since the first usage of indentation would be " *" on the second line of the comment
...after skipping initial comments then.
Define "comment" in a way that is language and file-format agnostic.
That seems like a can of worms to me, where you would perpetually be adding fixes to catch up with someone's edge case. A statistical approach would be inherently much more robust.
Could base on an existing SciTEBase::DiscoverIndentSetting
In Notepad++ the settings "Tab size" and "Replace by space" are too inflexible.
I often edit files from different sources / projects, which use various different conventions. It seems like my settings for tabs and spaces are always wrong when I open a file.
Meanwhile VSCode has a setting that is described like this: "Editor: Detect Indentation Controls whether [Editor: Tab Size] and [Editor: Insert Spaces] will be automatically detected when a file is opened based on the file contents."
This works well, and I wish Notepad++ had the same feature.