-
Notifications
You must be signed in to change notification settings - Fork 25
Show new lines, tabs, whitespaces #211
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| package service | ||
|
|
||
| import "github.com/pmezard/go-difflib/difflib" | ||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/pmezard/go-difflib/difflib" | ||
| ) | ||
|
|
||
| // Diff service generates diff for files | ||
| type Diff struct { | ||
|
|
@@ -12,8 +16,20 @@ func NewDiff() *Diff { | |
| return &Diff{context: 6} // keep it hard coded for now | ||
| } | ||
|
|
||
| // DiffPreprocessorFunc type is function signature to preprocess diffs | ||
| type DiffPreprocessorFunc func(string) string | ||
|
|
||
| // Generate return unified diff string for 2 files | ||
| func (d *Diff) Generate(nameA, nameB, contentA, contentB string) (string, error) { | ||
| func (d *Diff) Generate(nameA, nameB, contentA, contentB string, preprocessors ...DiffPreprocessorFunc) (string, error) { | ||
| for _, p := range preprocessors { | ||
| contentA = p(contentA) | ||
| contentB = p(contentB) | ||
| } | ||
|
|
||
| return d.generate(nameA, nameB, contentA, contentB) | ||
| } | ||
|
|
||
| func (d *Diff) generate(nameA, nameB, contentA, contentB string) (string, error) { | ||
| diff := difflib.UnifiedDiff{ | ||
| A: difflib.SplitLines(contentA), | ||
| B: difflib.SplitLines(contentB), | ||
|
|
@@ -24,3 +40,11 @@ func (d *Diff) Generate(nameA, nameB, contentA, contentB string) (string, error) | |
|
|
||
| return difflib.GetUnifiedDiffString(diff) | ||
| } | ||
|
|
||
| // ReplaceInvisible preprocessor function that replace invisible character with visible onces | ||
| func ReplaceInvisible(content string) string { | ||
| content = strings.Replace(content, " ", "·", -1) | ||
| content = strings.Replace(content, "\t", "→", -1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about replacing tab with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm used to seeing only one → symbol.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I definitely saw only → many times. |
||
| content = strings.Replace(content, "\r", "^M", -1) | ||
| return strings.Replace(content, "\n", "↵\n", -1) | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is duplicated some lines below https://github.com/src-d/code-annotation/pull/211/files#diff-fcf9e14a0be9f2c9ac7cd962eaa11d10R55