Wumpf / IncludeToolbox

Visual Studio extension to format, prune, and inspect include directives.
https://marketplace.visualstudio.com/items?itemName=Wumpf.IncludeToolbox
MIT License
51 stars 22 forks source link

Add new options for formatting relative include paths #47

Open dakotahawkins opened 7 years ago

dakotahawkins commented 7 years ago

NOTE: REVIEW ONLY PR: I still need to add tests for this

Setting PathFormat=PathMode.Absolute_FromParentDirWithFile in combination with setting FromParentDirWithFile to a filename that exists "toward" the root of your source directory tree allows us to make include paths relative to the nearest parent folder containing the specified file.

This is something we do with our projects in a global property sheet. MSBuild has the awesome built-in property function "GetDirectoryNameOfFileAbove", which lets you use a known file (e.g. we have an empty "build.root" in the root of our source tree) to do the same thing as this feature. Using this functionality lets you specify things like additional include/link dirs in a common way that works everywhere so you don't have to spam a lot of relative paths in your project settings or include directives.

In other words, with that set, any file can include any header by specifying its path relative to the root of our source.

To "correctly" handle system includes and includes that are in the same directory as the file you're working on (that we want to leave path-less), IgnoreFileRelative was changed from a boolean setting to an enum, where Never/Always should be the same as false/true and InSameDirectory/InSameOrSubDirectory provide some more control over which directives to skip.

I'm not entirely happy with the IgnoreFileRelative setting, I don't think it's very clear what it's supposed to do (in fact, I don't think I knew what it did before working on this), so perhaps this could be improved somehow in the future.

Wumpf commented 7 years ago

Phew that gave me something to think about :D

The main take away of my comments is that I think it's best to think of PathMode as a policy (out of the given paths which one is the best) and of AllowRelativePathMode/IgnoreFileRelativeMode as a constraint (which paths are we not allowed to choose).