Skip to content

Commit c99048f

Browse files
fix: fix matched label
1 parent aeef565 commit c99048f

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

src/dump_tree.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use web_tree_sitter_sg::{Point, TreeCursor};
99
#[derive(Deserialize, Serialize)]
1010
#[serde(rename_all = "camelCase")]
1111
pub struct DumpNode {
12-
id: u32,
1312
field: Option<String>,
1413
kind: String,
1514
start: Pos,
@@ -50,7 +49,6 @@ pub fn dump_one_node(cursor: &mut TreeCursor, target: &mut Vec<DumpNode>) {
5049
cursor.goto_parent();
5150
}
5251
target.push(DumpNode {
53-
id: node.id(),
5452
field,
5553
kind,
5654
start,

src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct WasmNode {
3030

3131
#[derive(Serialize, Deserialize)]
3232
pub struct WasmMatch {
33-
pub id: usize,
33+
pub kind: String,
3434
pub node: WasmNode,
3535
pub env: BTreeMap<String, WasmNode>,
3636
pub message: String,
@@ -47,12 +47,12 @@ fn get_message(rule: &RuleConfig<WasmLang>, node: &NodeMatch) -> String {
4747
impl WasmMatch {
4848
pub fn from_match(nm: NodeMatch, rule: &RuleConfig<WasmLang>) -> Self {
4949
let node = nm.get_node().clone();
50-
let id = node.node_id();
50+
let kind = node.kind().to_string();
5151
let node = WasmNode::from(node);
5252
let env = nm.get_env().clone();
5353
let env = env_to_map(env);
5454
let message = get_message(rule, &nm);
55-
Self { node, env, message, id }
55+
Self { node, env, message, kind }
5656
}
5757
}
5858

website/src/components/QueryEditor.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ watchEffect(() => {
2727
}
2828
})
2929
30+
// this assumes the node can be tracked by range + kind
31+
// it will break if multiple same-kind nodes have the same range
3032
const matchedIds = computed(() => {
3133
const matches = props.matches || []
32-
return new Set(matches.map(m => m.id))
34+
return new Set(matches.map(match => {
35+
const [startRow, startCol, endRow, endCol] = match.range
36+
return `${match.kind}-${startRow}-${startCol}-${endRow}-${endCol}`
37+
}))
3338
})
3439
3540
let cursorPosition = shallowRef<Pos>()

website/src/components/astGrep/lang.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ export type Match = {
5555
message: string,
5656
rule: string,
5757
env: any,
58-
id: number,
58+
kind: string,
5959
range: [number, number, number, number],
6060
} | {
6161
type: 'simple',
6262
env: any,
63-
id: number,
63+
kind: string,
6464
range: [number, number, number, number],
6565
}
6666

@@ -72,11 +72,23 @@ function shouldDisplayDiagnostic(rule: any) {
7272
)
7373
}
7474

75+
interface WasmNode {
76+
text: string
77+
range: [number, number, number, number]
78+
}
79+
80+
interface WasmMatch {
81+
kind: string
82+
node: WasmNode
83+
env: Map<string, WasmNode>
84+
message: string
85+
}
86+
7587
export async function doFind(src: string, json: any[]): Promise<[Match[], string]> {
7688
if (!src || !json) {
7789
return [[], src]
7890
}
79-
const result = await findNodes(src, json)
91+
const result: Map<string, WasmMatch[]> = await findNodes(src, json)
8092
let matches: Match[] = []
8193
for (let [ruleId, nodes] of result.entries()) {
8294
for (let rule of json) {
@@ -92,7 +104,7 @@ export async function doFind(src: string, json: any[]): Promise<[Match[], string
92104
message: nm.message,
93105
range: nm.node.range,
94106
env: nm.env,
95-
id: nm.id,
107+
kind: nm.kind,
96108
})
97109
}
98110
} else {
@@ -101,7 +113,7 @@ export async function doFind(src: string, json: any[]): Promise<[Match[], string
101113
type: 'simple',
102114
range: nm.node.range,
103115
env: nm.env,
104-
id: nm.id,
116+
kind: nm.kind,
105117
})
106118
}
107119
}

website/src/components/dump/TreeNode.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { PropType } from 'vue'
33
import { DumpNode, Pos, deepReactiveNode } from './dumpTree'
44
import { showToast } from '../utils/Toast.vue'
55
import GeneralNode from './GeneralNode.vue'
6+
import { computed } from 'vue';
67
78
const props = defineProps({
89
matchedIds: {
9-
type: Set as PropType<Set<number>>,
10+
type: Set as PropType<Set<string>>,
1011
required: true,
1112
},
1213
node: {
@@ -21,7 +22,6 @@ const props = defineProps({
2122
})
2223
2324
let {
24-
id,
2525
field,
2626
kind,
2727
start,
@@ -30,6 +30,15 @@ let {
3030
isNamed,
3131
} = deepReactiveNode(props)
3232
33+
// also see how matchedIds is computed
34+
// track nodes by range + kind
35+
const id = computed(() => {
36+
const k = kind.value
37+
const { row: startRow, column: startCol } = start.value
38+
const { row: endRow, column: endCol } = end.value
39+
return `${k}-${startRow}-${startCol}-${endRow}-${endCol}`
40+
})
41+
3342
function copyKind(kind: string) {
3443
navigator.clipboard.writeText(kind)
3544
showToast('Node kind copied!')

website/src/components/dump/dumpTree.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export interface GeneralNode {
1616
start: Pos
1717
end: Pos
1818
children: this[]
19-
id: number
2019
}
2120

2221
/** stub wasm DumpNode */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy