golang.org/x/tools/gopls@v0.15.3/internal/test/marker/testdata/highlight/highlight.txt (about) 1 This test checks basic functionality of the textDocument/highlight request. 2 3 -- highlights.go -- 4 package highlights 5 6 import ( 7 "fmt" //@loc(fmtImp, "\"fmt\""),highlight(fmtImp, fmtImp, fmt1, fmt2, fmt3, fmt4) 8 h2 "net/http" //@loc(hImp, "h2"),highlight(hImp, hImp, hUse) 9 "sort" 10 ) 11 12 type F struct{ bar int } //@loc(barDeclaration, "bar"),highlight(barDeclaration, barDeclaration, bar1, bar2, bar3) 13 14 func _() F { 15 return F{ 16 bar: 123, //@loc(bar1, "bar"),highlight(bar1, barDeclaration, bar1, bar2, bar3) 17 } 18 } 19 20 var foo = F{bar: 52} //@loc(fooDeclaration, "foo"),loc(bar2, "bar"),highlight(fooDeclaration, fooDeclaration, fooUse),highlight(bar2, barDeclaration, bar1, bar2, bar3) 21 22 func Print() { //@loc(printFunc, "Print"),highlight(printFunc, printFunc, printTest) 23 _ = h2.Client{} //@loc(hUse, "h2"),highlight(hUse, hImp, hUse) 24 25 fmt.Println(foo) //@loc(fooUse, "foo"),highlight(fooUse, fooDeclaration, fooUse),loc(fmt1, "fmt"),highlight(fmt1, fmtImp, fmt1, fmt2, fmt3, fmt4) 26 fmt.Print("yo") //@loc(printSep, "Print"),highlight(printSep, printSep, print1, print2),loc(fmt2, "fmt"),highlight(fmt2, fmtImp, fmt1, fmt2, fmt3, fmt4) 27 } 28 29 func (x *F) Inc() { //@loc(xRightDecl, "x"),loc(xLeftDecl, " *"),highlight(xRightDecl, xRightDecl, xUse),highlight(xLeftDecl, xRightDecl, xUse) 30 x.bar++ //@loc(xUse, "x"),loc(bar3, "bar"),highlight(xUse, xRightDecl, xUse),highlight(bar3, barDeclaration, bar1, bar2, bar3) 31 } 32 33 func testFunctions() { 34 fmt.Print("main start") //@loc(print1, "Print"),highlight(print1, printSep, print1, print2),loc(fmt3, "fmt"),highlight(fmt3, fmtImp, fmt1, fmt2, fmt3, fmt4) 35 fmt.Print("ok") //@loc(print2, "Print"),highlight(print2, printSep, print1, print2),loc(fmt4, "fmt"),highlight(fmt4, fmtImp, fmt1, fmt2, fmt3, fmt4) 36 Print() //@loc(printTest, "Print"),highlight(printTest, printFunc, printTest) 37 } 38 39 // DocumentHighlight is undefined, so its uses below are type errors. 40 // Nevertheless, document highlighting should still work. 41 //@diag(doc1, re"undefined|undeclared"), diag(doc2, re"undefined|undeclared"), diag(doc3, re"undefined|undeclared") 42 43 func toProtocolHighlight(rngs []int) []DocumentHighlight { //@loc(doc1, "DocumentHighlight"),loc(docRet1, "[]DocumentHighlight"),highlight(doc1, docRet1, doc1, doc2, doc3, result) 44 result := make([]DocumentHighlight, 0, len(rngs)) //@loc(doc2, "DocumentHighlight"),highlight(doc2, doc1, doc2, doc3) 45 for _, rng := range rngs { 46 result = append(result, DocumentHighlight{ //@loc(doc3, "DocumentHighlight"),highlight(doc3, doc1, doc2, doc3) 47 Range: rng, 48 }) 49 } 50 return result //@loc(result, "result") 51 } 52 53 func testForLoops() { 54 for i := 0; i < 10; i++ { //@loc(forDecl1, "for"),highlight(forDecl1, forDecl1, brk1, cont1) 55 if i > 8 { 56 break //@loc(brk1, "break"),highlight(brk1, forDecl1, brk1, cont1) 57 } 58 if i < 2 { 59 for j := 1; j < 10; j++ { //@loc(forDecl2, "for"),highlight(forDecl2, forDecl2, cont2) 60 if j < 3 { 61 for k := 1; k < 10; k++ { //@loc(forDecl3, "for"),highlight(forDecl3, forDecl3, cont3) 62 if k < 3 { 63 continue //@loc(cont3, "continue"),highlight(cont3, forDecl3, cont3) 64 } 65 } 66 continue //@loc(cont2, "continue"),highlight(cont2, forDecl2, cont2) 67 } 68 } 69 continue //@loc(cont1, "continue"),highlight(cont1, forDecl1, brk1, cont1) 70 } 71 } 72 73 arr := []int{} 74 for i := range arr { //@loc(forDecl4, "for"),highlight(forDecl4, forDecl4, brk4, cont4) 75 if i > 8 { 76 break //@loc(brk4, "break"),highlight(brk4, forDecl4, brk4, cont4) 77 } 78 if i < 4 { 79 continue //@loc(cont4, "continue"),highlight(cont4, forDecl4, brk4, cont4) 80 } 81 } 82 83 Outer: 84 for i := 0; i < 10; i++ { //@loc(forDecl5, "for"),highlight(forDecl5, forDecl5, brk5, brk6, brk8) 85 break //@loc(brk5, "break"),highlight(brk5, forDecl5, brk5, brk6, brk8) 86 for { //@loc(forDecl6, "for"),highlight(forDecl6, forDecl6, cont5), diag("for", re"unreachable") 87 if i == 1 { 88 break Outer //@loc(brk6, "break Outer"),highlight(brk6, forDecl5, brk5, brk6, brk8) 89 } 90 switch i { //@loc(switch1, "switch"),highlight(switch1, switch1, brk7) 91 case 5: 92 break //@loc(brk7, "break"),highlight(brk7, switch1, brk7) 93 case 6: 94 continue //@loc(cont5, "continue"),highlight(cont5, forDecl6, cont5) 95 case 7: 96 break Outer //@loc(brk8, "break Outer"),highlight(brk8, forDecl5, brk5, brk6, brk8) 97 } 98 } 99 } 100 } 101 102 func testSwitch() { 103 var i, j int 104 105 L1: 106 for { //@loc(forDecl7, "for"),highlight(forDecl7, forDecl7, brk10, cont6) 107 L2: 108 switch i { //@loc(switch2, "switch"),highlight(switch2, switch2, brk11, brk12, brk13) 109 case 1: 110 switch j { //@loc(switch3, "switch"),highlight(switch3, switch3, brk9) 111 case 1: 112 break //@loc(brk9, "break"),highlight(brk9, switch3, brk9) 113 case 2: 114 break L1 //@loc(brk10, "break L1"),highlight(brk10, forDecl7, brk10, cont6) 115 case 3: 116 break L2 //@loc(brk11, "break L2"),highlight(brk11, switch2, brk11, brk12, brk13) 117 default: 118 continue //@loc(cont6, "continue"),highlight(cont6, forDecl7, brk10, cont6) 119 } 120 case 2: 121 break //@loc(brk12, "break"),highlight(brk12, switch2, brk11, brk12, brk13) 122 default: 123 break L2 //@loc(brk13, "break L2"),highlight(brk13, switch2, brk11, brk12, brk13) 124 } 125 } 126 } 127 128 func testReturn() bool { //@loc(func1, "func"),loc(bool1, "bool"),highlight(func1, func1, fullRet11, fullRet12),highlight(bool1, bool1, false1, bool2, true1) 129 if 1 < 2 { 130 return false //@loc(ret11, "return"),loc(fullRet11, "return false"),loc(false1, "false"),highlight(ret11, func1, fullRet11, fullRet12) 131 } 132 candidates := []int{} 133 sort.SliceStable(candidates, func(i, j int) bool { //@loc(func2, "func"),loc(bool2, "bool"),highlight(func2, func2, fullRet2) 134 return candidates[i] > candidates[j] //@loc(ret2, "return"),loc(fullRet2, "return candidates[i] > candidates[j]"),highlight(ret2, func2, fullRet2) 135 }) 136 return true //@loc(ret12, "return"),loc(fullRet12, "return true"),loc(true1, "true"),highlight(ret12, func1, fullRet11, fullRet12) 137 } 138 139 func testReturnFields() float64 { //@loc(retVal1, "float64"),highlight(retVal1, retVal1, retVal11, retVal21) 140 if 1 < 2 { 141 return 20.1 //@loc(retVal11, "20.1"),highlight(retVal11, retVal1, retVal11, retVal21) 142 } 143 z := 4.3 //@loc(zDecl, "z") 144 return z //@loc(retVal21, "z"),highlight(retVal21, retVal1, retVal11, zDecl, retVal21) 145 } 146 147 func testReturnMultipleFields() (float32, string) { //@loc(retVal31, "float32"),loc(retVal32, "string"),highlight(retVal31, retVal31, retVal41, retVal51),highlight(retVal32, retVal32, retVal42, retVal52) 148 y := "im a var" //@loc(yDecl, "y"), 149 if 1 < 2 { 150 return 20.1, y //@loc(retVal41, "20.1"),loc(retVal42, "y"),highlight(retVal41, retVal31, retVal41, retVal51),highlight(retVal42, retVal32, yDecl, retVal42, retVal52) 151 } 152 return 4.9, "test" //@loc(retVal51, "4.9"),loc(retVal52, "\"test\""),highlight(retVal51, retVal31, retVal41, retVal51),highlight(retVal52, retVal32, retVal42, retVal52) 153 } 154 155 func testReturnFunc() int32 { //@loc(retCall, "int32") 156 mulch := 1 //@loc(mulchDec, "mulch"),highlight(mulchDec, mulchDec, mulchRet) 157 return int32(mulch) //@loc(mulchRet, "mulch"),loc(retFunc, "int32"),loc(retTotal, "int32(mulch)"),highlight(mulchRet, mulchDec, mulchRet),highlight(retFunc, retCall, retFunc, retTotal) 158 }