Skip to content

Commit

Permalink
parser:diff: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Jul 26, 2020
1 parent b5a6498 commit 20ba8a6
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions parser/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ func NewDiffParser(strip int) *DiffParser {
return p
}

// state data for a diagnostic.
type dstate struct {
startLine int
isInsert bool
newLines []string
originalLines []string // For Diagnostic.original_output
}

func (d dstate) build(path string, currentLine int) *rdf.Diagnostic {
drange := &rdf.Range{ // Diagnostic Range
Start: &rdf.Position{Line: int32(d.startLine)},
End: &rdf.Position{Line: int32(currentLine)},
}
text := strings.Join(d.newLines, "\n")
if d.isInsert {
text += "\n" // Need line-break at the end if it's insertion,
drange.GetEnd().Line = int32(d.startLine)
drange.GetEnd().Column = 1
drange.GetStart().Column = 1
}
return &rdf.Diagnostic{
Location: &rdf.Location{Path: path, Range: drange},
Suggestions: []*rdf.Suggestion{{Range: drange, Text: text}},
OriginalOutput: strings.Join(d.originalLines, "\n"),
}
}

// Parse parses input as unified diff format and return it as diagnostics.
func (p *DiffParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
filediffs, err := diff.ParseMultiFile(r)
Expand All @@ -38,62 +65,35 @@ func (p *DiffParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
for _, hunk := range fdiff.Hunks {
lnum := hunk.StartLineOld - 1
prevState := diff.LineUnchanged
var (
startLine int
isInsert bool
newLines []string
originalLines []string // For Diagnostic.original_output
)
reset := func() {
startLine = 0
isInsert = false
newLines = []string{}
originalLines = []string{}
}
state := dstate{}
emit := func() {
drange := &rdf.Range{ // Diagnostic Range
Start: &rdf.Position{Line: int32(startLine)},
End: &rdf.Position{Line: int32(lnum)},
}
text := strings.Join(newLines, "\n")
if isInsert {
text += "\n" // Need line-break at the end if it's insertion,
drange.GetEnd().Line = int32(startLine)
drange.GetEnd().Column = 1
drange.GetStart().Column = 1
}
d := &rdf.Diagnostic{
Location: &rdf.Location{Path: path, Range: drange},
Suggestions: []*rdf.Suggestion{{Range: drange, Text: text}},
OriginalOutput: strings.Join(originalLines, "\n"),
}
diagnostics = append(diagnostics, d)
reset()
diagnostics = append(diagnostics, state.build(path, lnum))
state = dstate{}
}
for i, diffLine := range hunk.Lines {
switch diffLine.Type {
case diff.LineAdded:
if i == 0 {
lnum++ // Increment line number only when it's at head.
}
newLines = append(newLines, diffLine.Content)
originalLines = append(originalLines, buildOriginalLine(path, diffLine))
state.newLines = append(state.newLines, diffLine.Content)
state.originalLines = append(state.originalLines, buildOriginalLine(path, diffLine))
switch prevState {
case diff.LineUnchanged:
// Insert.
startLine = lnum + 1
isInsert = true
state.startLine = lnum + 1
state.isInsert = true
case diff.LineDeleted, diff.LineAdded:
// Do nothing in particular.
}
case diff.LineDeleted:
lnum++
originalLines = append(originalLines, buildOriginalLine(path, diffLine))
state.originalLines = append(state.originalLines, buildOriginalLine(path, diffLine))
switch prevState {
case diff.LineUnchanged:
startLine = lnum
state.startLine = lnum
case diff.LineAdded:
isInsert = false
state.isInsert = false
case diff.LineDeleted:
// Do nothing in particular.
}
Expand All @@ -108,7 +108,7 @@ func (p *DiffParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
}
prevState = diffLine.Type
}
if startLine > 0 {
if state.startLine > 0 {
emit() // Output a diagnostic at the end of hunk.
}
}
Expand Down

0 comments on commit 20ba8a6

Please sign in to comment.