Skip to content

Commit

Permalink
Apply Code Review patches
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jul 11, 2019
1 parent ca0f73a commit 4342170
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
28 changes: 15 additions & 13 deletions integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,30 +308,31 @@ func (cfi *contextifyFramesIntegration) contextify(frames []Frame) []Frame {
}

var path string
// Optimize for happy path here
if fileExists(frame.AbsPath) {
path = frame.AbsPath

if cachedPath, ok := cfi.cachedLocations[frame.AbsPath]; ok {
path = cachedPath
} else {
path = cfi.findNearbySourceCodeLocation(frame.AbsPath)
// Optimize for happy path here
if fileExists(frame.AbsPath) {
path = frame.AbsPath
} else {
path = cfi.findNearbySourceCodeLocation(frame.AbsPath)
}
}

if path == "" {
contextifiedFrames = append(contextifiedFrames, frame)
continue
}

lines, initial := cfi.sr.readContextLines(path, frame.Lineno, cfi.contextLines)
contextifiedFrames = append(contextifiedFrames, cfi.addContextLinesToFrame(frame, lines, initial))
lines, context_line := cfi.sr.readContextLines(path, frame.Lineno, cfi.contextLines)
contextifiedFrames = append(contextifiedFrames, cfi.addContextLinesToFrame(frame, lines, context_line))
}

return contextifiedFrames
}

func (cfi *contextifyFramesIntegration) findNearbySourceCodeLocation(originalPath string) string {
if cachedPath, ok := cfi.cachedLocations[originalPath]; ok {
return cachedPath
}

trimmedPath := strings.TrimPrefix(originalPath, "/")
components := strings.Split(trimmedPath, "/")

Expand All @@ -345,15 +346,16 @@ func (cfi *contextifyFramesIntegration) findNearbySourceCodeLocation(originalPat
}
}

cfi.cachedLocations[originalPath] = ""
return ""
}

func (cfi *contextifyFramesIntegration) addContextLinesToFrame(frame Frame, lines [][]byte, initial int) Frame {
func (cfi *contextifyFramesIntegration) addContextLinesToFrame(frame Frame, lines [][]byte, context_line int) Frame {
for i, line := range lines {
switch {
case i < initial:
case i < context_line:
frame.PreContext = append(frame.PreContext, string(line))
case i == initial:
case i == context_line:
frame.ContextLine = string(line)
default:
frame.PostContext = append(frame.PostContext, string(line))
Expand Down
10 changes: 5 additions & 5 deletions sourcereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ func (sr *sourceReader) readContextLines(filename string, line, context int) ([]
return sr.calculateContextLines(lines, line, context)
}

// `initial` points to a line that's the `context_line` itself in relation to returned slice
// `context_line` points to a line that caused an issue itself, in relation to returned slice
func (sr *sourceReader) calculateContextLines(lines [][]byte, line, context int) ([][]byte, int) {
// Stacktrace lines are 1-indexed, slices are 0-indexed
line--

initial := context
context_line := context

if lines == nil || line >= len(lines) || line < 0 {
return nil, 0
}

if context < 0 {
context = 0
initial = 0
context_line = 0
}

start := line - context

if start < 0 {
initial += start
context_line += start
start = 0
}

Expand All @@ -65,5 +65,5 @@ func (sr *sourceReader) calculateContextLines(lines [][]byte, line, context int)
end = len(lines)
}

return lines[start:end], initial
return lines[start:end], context_line
}

0 comments on commit 4342170

Please sign in to comment.