Day 31: Making a Mega Diagnostic
🥇 Mood: Celebrating that Henry won Big’s Backyard Ultra after 85 hours and 354 miles.
🎵 Soundtrack: “wfh but make it jazz” playlist
📚 Reading Github Issues
Nothing exciting.
🩺 Making My Own Diagnostic
I improved my diagnostic! Now it replaces things and looks pretty!
Here is my code
var Analyzer = &analysis.Analyzer{
Name: "simplifyamelia",
Doc: Doc,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{(*ast.BasicLit)(nil)}
diagnostics := []analysis.Diagnostic{}
inspect.Preorder(nodeFilter, func(n ast.Node) {
cmt := n.(*ast.BasicLit)
if cmt.Kind == token.STRING && strings.Contains(cmt.Value, "amelia") {
newText := strings.ReplaceAll(cmt.Value, "amelia", "ameowlia")
d := createDiagnostic(pass, cmt.Pos(), cmt.End(), newText)
pass.Report(d)
}
})
return nil, nil
}
func createDiagnostic(pass *analysis.Pass, start, end token.Pos, newText string) analysis.Diagnostic {
return analysis.Diagnostic{
Pos: start,
End: end,
Message: "Fix 'amelia'",
SuggestedFixes: []analysis.SuggestedFix,
}},
}
}
My next goal is to make a diagnostic that combines all of these fixes. (I’m inching closer to the actual issue I want to solve.)
I thought it would be as simple as adding a mega diagnostic that combines all of the text edits from the other diagnostics. This did work…but it’s not exactly what I want. That ended up showing up all the time, and it was awkward.
Currently when I highlight text a new option comes up that says “extract function”. I want something like that where it only appears when you highlight over text.
I started looking at how “extract function” worked, but didn’t get too far before other work things call me away.