1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package analysis 6 7 import "go/token" 8 9 // A Diagnostic is a message associated with a source location or range. 10 // 11 // An Analyzer may return a variety of diagnostics; the optional Category, 12 // which should be a constant, may be used to classify them. 13 // It is primarily intended to make it easy to look up documentation. 14 // 15 // If End is provided, the diagnostic is specified to apply to the range between 16 // Pos and End. 17 type Diagnostic struct { 18 Pos token.Pos 19 End token.Pos // optional 20 Category string // optional 21 Message string 22 23 // URL is the optional location of a web page that provides 24 // additional documentation for this diagnostic. 25 // 26 // If URL is empty but a Category is specified, then the 27 // Analysis driver should treat the URL as "#"+Category. 28 // 29 // The URL may be relative. If so, the base URL is that of the 30 // Analyzer that produced the diagnostic; 31 // see https://pkg.go.dev/net/url#URL.ResolveReference. 32 URL string 33 34 // SuggestedFixes is an optional list of fixes to address the 35 // problem described by the diagnostic, each one representing 36 // an alternative strategy; at most one may be applied. 37 SuggestedFixes []SuggestedFix 38 39 // Related contains optional secondary positions and messages 40 // related to the primary diagnostic. 41 Related []RelatedInformation 42 } 43 44 // RelatedInformation contains information related to a diagnostic. 45 // For example, a diagnostic that flags duplicated declarations of a 46 // variable may include one RelatedInformation per existing 47 // declaration. 48 type RelatedInformation struct { 49 Pos token.Pos 50 End token.Pos // optional 51 Message string 52 } 53 54 // A SuggestedFix is a code change associated with a Diagnostic that a 55 // user can choose to apply to their code. Usually the SuggestedFix is 56 // meant to fix the issue flagged by the diagnostic. 57 // 58 // The TextEdits must not overlap, nor contain edits for other packages. 59 type SuggestedFix struct { 60 // A description for this suggested fix to be shown to a user deciding 61 // whether to accept it. 62 Message string 63 TextEdits []TextEdit 64 } 65 66 // A TextEdit represents the replacement of the code between Pos and End with the new text. 67 // Each TextEdit should apply to a single file. End should not be earlier in the file than Pos. 68 type TextEdit struct { 69 // For a pure insertion, End can either be set to Pos or token.NoPos. 70 Pos token.Pos 71 End token.Pos 72 NewText []byte 73 } 74