@@ -2,7 +2,16 @@ local api = vim.api
22local keymap = vim .keymap
33local utils = require (" wildfire.utils" )
44local surround = require (" wildfire.surround" )
5- local ts_select = require (" vim.treesitter._select" )
5+ local textobjects = require (" wildfire.textobjects" )
6+
7+ local ok_tss , ts_select = pcall (require , " vim.treesitter._select" )
8+ if not ok_tss then ts_select = nil end
9+
10+ local function use_treesitter (buf )
11+ if not ts_select then return false end
12+ local ok , parser = pcall (vim .treesitter .get_parser , buf )
13+ return ok and parser ~= nil
14+ end
615
716local M = {}
817
@@ -57,11 +66,20 @@ local function find_node_at_range(buf, sr, sc, er, ec)
5766end
5867
5968function M .init_selection ()
60- local node = vim .treesitter .get_node ({ ignore_injections = false })
61- if not node then return end
6269 local buf = api .nvim_get_current_buf ()
6370 checkpoint :reset (buf )
64- update_selection_by_node (node )
71+
72+ if use_treesitter (buf ) then
73+ local node = vim .treesitter .get_node ({ ignore_injections = false })
74+ if not node then return end
75+ update_selection_by_node (node )
76+ else
77+ local range = textobjects .init ()
78+ if not range then return end
79+ utils .update_selection (buf , range )
80+ checkpoint :save (buf , range )
81+ end
82+
6583 for _ = 1 , vim .v .count1 - 1 do
6684 M .node_incremental ()
6785 end
7088function M .node_incremental ()
7189 local buf = api .nvim_get_current_buf ()
7290
73- -- Initialize from current visual selection when no prior state
74- if not checkpoint :has_state (buf ) then
75- local csrow , cscol , cerow , cecol = utils .visual_selection_range ()
76- local ok , parser = pcall (vim .treesitter .get_parser , buf )
77- if not ok or not parser then return end
78- local tree = parser :parse ()[1 ]
79- if not tree then return end
80- local node = tree :root ():named_descendant_for_range (csrow - 1 , cscol - 1 , cerow - 1 , cecol )
81- if node then update_selection_by_node (node ) end
82- return
83- end
91+ if use_treesitter (buf ) then
92+ -- Initialize from current visual selection when no prior state
93+ if not checkpoint :has_state (buf ) then
94+ local csrow , cscol , cerow , cecol = utils .visual_selection_range ()
95+ local ok , parser = pcall (vim .treesitter .get_parser , buf )
96+ if not ok or not parser then return end
97+ local tree = parser :parse ()[1 ]
98+ if not tree then return end
99+ local node = tree :root ():named_descendant_for_range (csrow - 1 , cscol - 1 , cerow - 1 , cecol )
100+ if node then update_selection_by_node (node ) end
101+ return
102+ end
84103
85- local csrow , cscol , cerow , cecol = utils .visual_selection_range ()
86- ts_select .select_parent (1 )
87- local nsr , nsc , ner , nec = utils .visual_selection_range ()
104+ local csrow , cscol , cerow , cecol = utils .visual_selection_range ()
105+ ts_select .select_parent (1 )
106+ local nsr , nsc , ner , nec = utils .visual_selection_range ()
88107
89- -- No change → at tree root
90- if nsr == csrow and nsc == cscol and ner == cerow and nec == cecol then
91- return
92- end
108+ -- No change → at tree root
109+ if nsr == csrow and nsc == cscol and ner == cerow and nec == cecol then
110+ return
111+ end
93112
94- local node = find_node_at_range (buf , nsr , nsc , ner , nec )
95- if node then
96- update_selection_by_node (node )
113+ local node = find_node_at_range (buf , nsr , nsc , ner , nec )
114+ if node then
115+ update_selection_by_node (node )
116+ else
117+ utils .update_selection (buf , { nsr , nsc , ner , nec })
118+ checkpoint :save (buf , { nsr , nsc , ner , nec })
119+ end
97120 else
98- utils .update_selection (buf , { nsr , nsc , ner , nec })
99- checkpoint :save (buf , { nsr , nsc , ner , nec })
121+ local csrow , cscol , cerow , cecol = utils .visual_selection_range ()
122+ local range = textobjects .expand (buf , csrow , cscol , cerow , cecol )
123+ if range then
124+ utils .update_selection (buf , range )
125+ checkpoint :save (buf , range )
126+ end
100127 end
101128end
102129
0 commit comments