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

Rule to compare selection. #69

Closed carlosmgilp closed 3 years ago

carlosmgilp commented 3 years ago

Is there any way that I can compare two pieces of code, but it is only the code, not what line it is on. The code is from Autocad autolisp extension. I have two code snippets

;#region check ss
; Selection
(defun selectionlisp1 (/ sset check) 
  ;load the visual lisp extensions
  (vl-load-com)
  ;check for selection
  (while (setq sset (car (entsel))) 
    (setq sset (vlax-ename->vla-object sset))
    (setq check (vlax-property-available-p sset "Color" T))
    (if check 
      (vlax-put-property sset 'Color 4)))
  (princ))
;#endregion

;#region check ss
; Selection
(defun selectionlisp1 (/ sset check) 
  ;load the visual lisp extensions
  (vl-load-com)
  ;check for selection
  (while (setq sset (car (entsel))) 
    (setq sset (vlax-ename->vla-object sset))
    (setq check (vlax-property-available-p sset "Color" T))
    (if check (vlax-put-property sset 'Color 4)))
  (princ))
;#endregion

Both are the same, it only changes that one line is on another, but the code still works. Comparing the selections shows me a difference, but there should be no difference because the code is the same. How can I improve this. Thanks.

ryu1kn commented 3 years ago

Hi @carlosmgilp , Partial diff doesn't know if those lines are semantically the same, as it only does text comparison.

However, if just ignoring the line break difference does the job, you can use partialDiff.preComparisonTextNormalizationRules with something like this:

  "partialDiff.preComparisonTextNormalizationRules": [
    {
      "name": "Ignore line breaks",
      "match": "\n+",
      "replaceWith": " "
    }
  ]
carlosmgilp commented 3 years ago

Hello @ryu1kn how are you. Thanks for your answer.

I don't really understand how the rule works.

  1. I keep the rule.

01

  1. I verify that it is active.

02

  1. Here I show where the line that goes down is.

03

  1. I select the text to compare.

04

  1. I compare with the previous selection.

05

  1. But it still shows me the difference.

06

Could you explain to me what I'm doing wrong. Thank you in advance.

ryu1kn commented 3 years ago

Ah, in your first comment, all lines didn't have indentations. I've updated it to use a code block to keep the indentations.

With the leading indentation, the regex I posted above doesn't work. Something like this would do "match": " *\n +". The comparison result should look like this (no difference).

image

carlosmgilp commented 3 years ago

Hi @ryu1kn , thanks again for your time to help me.

But it doesn't work for me.

Save the new rule.

07

Verify that it was active.

08

I made the previous selection. Then select the text to compare. And this is the result it gives me.

image

In what you can help me, I appreciate it.

ryu1kn commented 3 years ago

Are you using Windows? Is your line ending character CRLF but not LF? In which case, the regex needs to be "match": " *\r\n +"

carlosmgilp commented 3 years ago

Hello Brother how are you. Excuse me for not specifying the operating system. I work with windows. Thank you. Now if it works very well. I don't understand why it puts all the code on one line. If that could be improved in the future, it would be much better. If it were shown as the example image it would be spectacular.

13

Thank you very much for helping me, for your time and dedication. Greetings and many successes.

ryu1kn commented 3 years ago

@carlosmgilp It puts all the code on one line because the below normalisation rule instructs to replace strings that match regex *\r\n + to a single space character. We can improve this normalisation rule but as the extension doesn't know the language syntax (it doesn't have a lisp parser), it would be limited.

  "partialDiff.preComparisonTextNormalizationRules": [
    {
      "name": "Ignore line breaks",
      "match": " *\r\n +",
      "replaceWith": " "
    }
  ]
ryu1kn commented 3 years ago

I'm closing this...