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