ryu1kn / vscode-partial-diff

Visual Studio Code Extension. Take a diff of 2 parts of text(s)
https://marketplace.visualstudio.com/items?itemName=ryu1kn.partial-diff
MIT License
184 stars 15 forks source link

Feature suggest: ignore white space #15

Closed stepbeta closed 6 years ago

stepbeta commented 7 years ago

It would be incredibly helpful to have a way to specify whether to ignore white space/blank lines changes or not.

This could either be a setting in VSCode (global or workspace) or, probably even better, a checkbox in the result tab one could toggle while seeing the results.

ryu1kn commented 6 years ago

Hi @stepbeta , sorry for my late reply. Somehow I missed the issue.

With this feature, do you want to say, "There is no significant difference between those 2 text. BTW I'm ignoring space characters" ?

Sounds useful. But probably we need to normalise 2 texts before we compare. The texts we show in the diff view will be not the original one but normalised one. Does it work for you?

What do you want with "ignore white space / blank lines"? Do you want treat a___b and a_b the same, but not a_b and ab? (here _ means space character)

stepbeta commented 6 years ago

Hi @ryu1kn , thank you for your work!

What I had in mind is a way to wade through the noise, so to speak. Say, sometimes in a file you only have indentation changes, or spaces before/inside/after braces. E.g.:

// before
function() {

// after
function () {

The only thing that changes is the space between function and its parenthesis. In this sense, a__b and a_b should be the same, but as you justly say a_b and ab are different. This should work the same with spaces and tabs (because for example somebody changed indentation from tabs to 4 spaces or something like that, as a use case). In a file with perhaps hundreds of lines, if I could skip half of it because the only change is spaces, it'd surely be more pleasant and more to the point of what I'm looking for in the diff.

As per blank lines, those should be only lines added or removed but without content in them.

// before
function () {
  console.log();

// after
function () {
+
  console.log()

What would be real useful would be a way to toggle this way of diff'ing the texts. See this screenshot for example: there's a checkbox that lets you toggle exactly this feature.

ryu1kn commented 6 years ago

Thanks for the detailed explanation, @stepbeta . I got your point.

Hmm... so you want to treat function () and function() the same, but async myFunction and asyncmyFunction different. So just collapsing multiple space characters is not enough. I feel it would be difficult to provide one ignore space characters rule that works for almost everyone. I'm wondering if I can provide a mechanism that people can write their own normalisation rules that get executed before a text comparison happens ๐Ÿค”

ryu1kn commented 6 years ago

provide a mechanism that people can write their own normalisation rules that get executed before a text comparison happens

Then it can help this request too: https://github.com/ryu1kn/vscode-partial-diff/issues/10

ryu1kn commented 6 years ago

For example, allow this sort of user settings.

    "partialDiff.preComparisonTextProcessRules": [
      {
        "name": "Replace tabs with whitespaces",
        "rule": "({text} => text.replace(/\\t/g, '  ')"
      },
      {
        "name": "Squash whitespaces",
        "rule": "({text}) => text.replace(/ +/g, ' ')"
      },
      {
        "name": "Remove spaces before opening parenthsis",
        "rule": "({text}) => text.replace(/\\s+\\(/, '(')"
      },
      {
        "name": "Remove empty lines",
        "rule": "({text}) => text.replace(/\\n\\n+/g, '\\n')"
      }
    ]
ryu1kn commented 6 years ago

Memo:

Now the comparison view's title is of the format file1 โ†” file2. I wanna know if the texts could have been normalised before the comparison, so want to change the title accordingly, like file1* โ†” file2* or file1 โˆผ file2.

ryu1kn commented 6 years ago

The use cases we see so far doesnโ€™t require arbitrary functions to normalise texts, i now think we should just accept arguments for String#replace method.

ryu1kn commented 6 years ago

Then setting might look like this:

"partialDiff.preComparisonTextProcessRules": [
  {
    "name": "Replace tabs with whitespaces",
    "match": "\t",
    "replaceWith": "  ",
  },
  {
    "name": "Squash whitespaces",
    "match": " +",
    "replaceWith": " "
  },
  {
    "name": "Remove spaces before opening parenthsis",
    "match": "\\s+(",
    "replaceWith": "("
  },
  {
    "name": "Remove empty lines",
    "match": "\n\n",
    "replaceWith": "\n"
  }
]
ryu1kn commented 6 years ago

Then it can help this request too: #10

But with String#replace, I cannot save #10 case ๐Ÿ˜ , like capitalising text

Maybe extend replaceWith in that case:

"replaceWith": "new text"

"replaceWith": "$1"

"replaceWith": {
  "expression": "$1",
  "letterCase": "upper"
}
ryu1kn commented 6 years ago
"replaceWith": {
  "expression": "$1",
  "letterCase": "upper"
}

From the implementation point of view, let's accept either just replace text or letterCase for now. So, if someone really wants to replace a text and change them to upper case, they list 2 rules.

"replaceWith": "new text"               // Simple text
"replaceWith": "$1, $2"                 // Replace with matched texts
"replaceWith": {"letterCase": "upper"}  // Change all characters capital
ryu1kn commented 6 years ago

Released as v0.5.0! @stepbeta would you like to test it out if you can now reduce the noise in the diff?

stepbeta commented 6 years ago

Hi, just seen the update in VS Code, I'll give it a spin and let you know! Thank you for this!

stepbeta commented 6 years ago

Looks great! Really good job, thanks!

If I can push it a little further, would it be possible to enable/disable the rules while in the diff view? Perhaps a little menu with the list of rules and the ability to check/uncheck each one or all of them?

The reason behind this is because, if I diff two files rather than just a few lines, sometimes I want to actually see if the only differences are the ones "hidden" by the rules, and being able to toggle between the two versions (original/normalized) would be incredibly useful!

ryu1kn commented 6 years ago

Thanks for testing it out! I'm closing this issue then ๐Ÿ˜‰

As for your suggestion, I think that will be a very helpful feature to have. I would also want to quickly check how many more differences I get if I toggle off certain rules. Would you mind raising another issue for that functionality? Thank you!