Skip to content

Commit b068b33

Browse files
committed
update demo output
Signed-off-by: Pranav Gaikwad <[email protected]>
1 parent e85543a commit b068b33

File tree

5 files changed

+148
-52
lines changed

5 files changed

+148
-52
lines changed

demo-local.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ COPY provider_pod_local_settings.json /analyzer-lsp/provider_settings.json
1010

1111
RUN python3 -m venv /analyzer-lsp/examples/python/.venv
1212
RUN yes | python3 -m pip install -r /analyzer-lsp/examples/python/requirements.txt
13+
RUN cd /analyzer-lsp/examples/nodejs && npm install
1314

1415
EXPOSE 16686
1516

demo-output.yaml

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -600,19 +600,19 @@
600600
- uri: file:///examples/nodejs/test_a.ts
601601
message: nodejs sample rule 001
602602
codeSnip: " 1 export interface Greeter {\n 2 name: string;\n 3 hello(): string;\n 4 }\n 5 \n 6 export const greeter: Greeter = {\n 7 name: \"Person1\",\n 8 hello() {\n 9 return `Hello, I'm ${this.name}`;\n10 },\n11 };\n"
603-
lineNumber: 5
603+
lineNumber: 6
604604
variables:
605605
file: file:///examples/nodejs/test_a.ts
606606
- uri: file:///examples/nodejs/test_b.ts
607607
message: nodejs sample rule 001
608608
codeSnip: " 1 import { greeter } from './test_a';\n 2 \n 3 console.log(greeter.hello());\n"
609-
lineNumber: 0
609+
lineNumber: 1
610610
variables:
611611
file: file:///examples/nodejs/test_b.ts
612612
- uri: file:///examples/nodejs/test_b.ts
613613
message: nodejs sample rule 001
614614
codeSnip: " 1 import { greeter } from './test_a';\n 2 \n 3 console.log(greeter.hello());\n"
615-
lineNumber: 2
615+
lineNumber: 3
616616
variables:
617617
file: file:///examples/nodejs/test_b.ts
618618
effort: 1
@@ -623,19 +623,30 @@
623623
- uri: file:///examples/nodejs/test_a.ts
624624
message: nodejs sample rule 002
625625
codeSnip: " 1 export interface Greeter {\n 2 name: string;\n 3 hello(): string;\n 4 }\n 5 \n 6 export const greeter: Greeter = {\n 7 name: \"Person1\",\n 8 hello() {\n 9 return `Hello, I'm ${this.name}`;\n10 },\n11 };\n"
626-
lineNumber: 2
626+
lineNumber: 3
627627
variables:
628628
file: file:///examples/nodejs/test_a.ts
629629
- uri: file:///examples/nodejs/test_a.ts
630630
message: nodejs sample rule 002
631631
codeSnip: " 1 export interface Greeter {\n 2 name: string;\n 3 hello(): string;\n 4 }\n 5 \n 6 export const greeter: Greeter = {\n 7 name: \"Person1\",\n 8 hello() {\n 9 return `Hello, I'm ${this.name}`;\n10 },\n11 };\n"
632-
lineNumber: 7
632+
lineNumber: 8
633633
variables:
634634
file: file:///examples/nodejs/test_a.ts
635635
- uri: file:///examples/nodejs/test_b.ts
636636
message: nodejs sample rule 002
637637
codeSnip: " 1 import { greeter } from './test_a';\n 2 \n 3 console.log(greeter.hello());\n"
638-
lineNumber: 2
638+
lineNumber: 3
639+
variables:
640+
file: file:///examples/nodejs/test_b.ts
641+
effort: 1
642+
node-sample-rule-003:
643+
description: Testing that the node module files are not matched
644+
category: potential
645+
incidents:
646+
- uri: file:///examples/nodejs/test_b.ts
647+
message: nodejs sample rule 003
648+
codeSnip: " 1 import { greeter } from './test_a';\n 2 \n 3 console.log(greeter.hello());\n"
649+
lineNumber: 3
639650
variables:
640651
file: file:///examples/nodejs/test_b.ts
641652
effort: 1
@@ -785,9 +796,15 @@
785796
- uri: file:///examples/nodejs/Component.tsx
786797
message: Found React reference using nodejs provider
787798
codeSnip: " 1 import React from 'react';\n 2 \n 3 export const MyComponent: React.FC = () => {\n 4 return <div>Hello from TypeScript React</div>;\n 5 };\n"
788-
lineNumber: 2
799+
lineNumber: 1
789800
variables:
790801
file: file:///examples/nodejs/Component.tsx
802+
- uri: file:///examples/nodejs/LegacyComponent.jsx
803+
message: Found React reference using nodejs provider
804+
codeSnip: " 1 import React from 'react';\n 2 \n 3 export const LegacyComponent = () => {\n 4 return <div>Hello from JavaScript React</div>;\n 5 };\n"
805+
lineNumber: 1
806+
variables:
807+
file: file:///examples/nodejs/LegacyComponent.jsx
791808
effort: 1
792809
xml-pom-001:
793810
description: ""
@@ -1353,5 +1370,4 @@
13531370
unmatched:
13541371
- file-002
13551372
- lang-ref-002
1356-
- node-sample-rule-003
13571373
- python-sample-rule-003

examples/nodejs/package-lock.json

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/nodejs/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"@types/node": "^24.10.1",
4+
"@types/react": "^19.2.6",
5+
"react": "^19.2.0"
6+
}
7+
}

lsp/base_service_client/base_service_client.go

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,15 @@ func (sc *LSPServiceClientBase) populateDocumentSymbolCache(ctx context.Context,
535535
sc.Log.Error(err, "documentSymbol request failed", "uri", uri)
536536
continue
537537
}
538-
for _, symbol := range findDocumentSymbolsAtLocation(uri, documentSymbols, definition) {
538+
if symbol, ok := findDocumentSymbolAtLocation(uri, documentSymbols, definition); ok {
539539
wsSymbolsForDefinitions[workspaceSymbolKey(symbol)] = symbol
540540
}
541541
}
542542
definitionSymbols := []protocol.WorkspaceSymbol{}
543543
for _, symbol := range wsSymbolsForDefinitions {
544544
definitionSymbols = append(definitionSymbols, symbol)
545545
}
546-
// attach all definitions found with the original match, so that
547-
// we can determine it as a referenced symbol
546+
// attach all definitions found with the original match
548547
pair := WorkspaceSymbolDefinitionsPair{
549548
WorkspaceSymbol: protocol.WorkspaceSymbol{
550549
Location: protocol.OrPLocation_workspace_symbol{
@@ -564,6 +563,7 @@ func (sc *LSPServiceClientBase) populateDocumentSymbolCache(ctx context.Context,
564563
}
565564
workspaceSymbols[workspaceSymbolKey(pair.WorkspaceSymbol)] = pair
566565
}
566+
567567
workspaceSymbolsList := []WorkspaceSymbolDefinitionsPair{}
568568
for _, pair := range workspaceSymbols {
569569
workspaceSymbolsList = append(workspaceSymbolsList, pair)
@@ -725,19 +725,19 @@ func (sc *LSPServiceClientBase) searchContentForWorkspaceSymbols(ctx context.Con
725725
},
726726
},
727727
}
728-
matchingSymbols := findDocumentSymbolsAtLocation(fileURI, symbols, protoLoc)
729-
positions = append(positions, matchingSymbols...)
730-
positions = append(positions, protocol.WorkspaceSymbol{
731-
Location: protocol.OrPLocation_workspace_symbol{
732-
Value: protoLoc,
733-
},
734-
BaseSymbolInformation: protocol.BaseSymbolInformation{
735-
Name: scanner.Text()[loc[0]:loc[1]],
736-
Kind: 0,
737-
Tags: []protocol.SymbolTag{},
738-
ContainerName: "",
739-
},
740-
})
728+
if symbol, ok := findDocumentSymbolAtLocation(fileURI, symbols, protoLoc); ok {
729+
positions = append(positions, symbol)
730+
} else {
731+
positions = append(positions, protocol.WorkspaceSymbol{
732+
Location: protocol.OrPLocation_workspace_symbol{
733+
Value: protoLoc,
734+
},
735+
BaseSymbolInformation: protocol.BaseSymbolInformation{
736+
Name: scanner.Text()[loc[0]:loc[1]],
737+
Kind: 0,
738+
},
739+
})
740+
}
741741
}
742742
lineNumber++
743743
}
@@ -772,37 +772,44 @@ func (sc *LSPServiceClientBase) getDefinitionForPosition(ctx context.Context, ur
772772
return res
773773
}
774774

775-
func findDocumentSymbolsAtLocation(docURI uri.URI, symbols []protocol.DocumentSymbol, defLoc protocol.Location) []protocol.WorkspaceSymbol {
776-
var out []protocol.WorkspaceSymbol
777-
findSymbolsAtLocationRecursive(docURI, symbols, defLoc, "", &out)
778-
return out
779-
}
780-
781-
func findSymbolsAtLocationRecursive(docURI uri.URI, symbols []protocol.DocumentSymbol, defLoc protocol.Location, containerName string, out *[]protocol.WorkspaceSymbol) {
782-
for _, symbol := range symbols {
783-
symRange := preferredRange(symbol)
784-
if rangeOverlaps(symRange, defLoc.Range) {
785-
ws := protocol.WorkspaceSymbol{
786-
BaseSymbolInformation: protocol.BaseSymbolInformation{
787-
Name: symbol.Name,
788-
Kind: symbol.Kind,
789-
Tags: symbol.Tags,
790-
ContainerName: containerName,
791-
},
792-
Location: protocol.OrPLocation_workspace_symbol{
793-
Value: protocol.Location{
794-
URI: protocol.DocumentURI(docURI),
795-
Range: symRange,
796-
},
797-
},
775+
func findDocumentSymbolAtLocation(docURI uri.URI, symbols []protocol.DocumentSymbol, defLoc protocol.Location) (protocol.WorkspaceSymbol, bool) {
776+
var bestSymbol protocol.WorkspaceSymbol
777+
var bestLength uint64
778+
found := false
779+
780+
var traverse func([]protocol.DocumentSymbol, string)
781+
traverse = func(symbols []protocol.DocumentSymbol, containerName string) {
782+
for _, symbol := range symbols {
783+
symRange := preferredRange(symbol)
784+
if rangeOverlaps(symRange, defLoc.Range) {
785+
length := rangeLength(symRange)
786+
if !found || length < bestLength {
787+
bestSymbol = protocol.WorkspaceSymbol{
788+
BaseSymbolInformation: protocol.BaseSymbolInformation{
789+
Name: symbol.Name,
790+
Kind: symbol.Kind,
791+
Tags: symbol.Tags,
792+
ContainerName: containerName,
793+
},
794+
Location: protocol.OrPLocation_workspace_symbol{
795+
Value: protocol.Location{
796+
URI: protocol.DocumentURI(docURI),
797+
Range: symRange,
798+
},
799+
},
800+
}
801+
bestLength = length
802+
found = true
803+
}
804+
}
805+
if len(symbol.Children) > 0 {
806+
traverse(symbol.Children, symbol.Name)
798807
}
799-
*out = append(*out, ws)
800-
}
801-
// Traverse children
802-
if len(symbol.Children) > 0 {
803-
findSymbolsAtLocationRecursive(docURI, symbol.Children, defLoc, symbol.Name, out)
804808
}
805809
}
810+
811+
traverse(symbols, "")
812+
return bestSymbol, found
806813
}
807814

808815
func rangeOverlaps(r1, r2 protocol.Range) bool {
@@ -825,3 +832,15 @@ func positionLessEqual(p1, p2 protocol.Position) bool {
825832
}
826833
return false
827834
}
835+
836+
func rangeLength(r protocol.Range) uint64 {
837+
lineDiff := int64(r.End.Line) - int64(r.Start.Line)
838+
if lineDiff < 0 {
839+
lineDiff = 0
840+
}
841+
charDiff := int64(r.End.Character) - int64(r.Start.Character)
842+
if charDiff < 0 {
843+
charDiff = 0
844+
}
845+
return (uint64(lineDiff) << 32) | uint64(charDiff)
846+
}

0 commit comments

Comments
 (0)