-
Notifications
You must be signed in to change notification settings - Fork 0
/
]
256 lines (235 loc) · 6.25 KB
/
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
local actions = require "telescope.actions"
local action_state = require("telescope.actions.state")
local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local conf = require("telescope.config").values
local bufnr = 0
local function get_latex_element(query_string)
local parser = vim.treesitter.get_parser(bufnr, "latex")
local root = parser:parse()[1]:root()
local query = vim.treesitter.query.parse('latex', query_string)
local result = {}
for _,match,_ in query:iter_matches(root,bufnr, 0, -1) do
for _, node in pairs(match) do
local text = vim.treesitter.get_node_text(node,0)
local line = node:range()+1
table.insert(result,{
text = text,
line = line})
end
end
return result
end
local function get_newcommands()
local query = "(new_command_definition) @new_command"
local commands = {}
local results = get_latex_element(query)
for _,result in pairs(results) do
local entry = {
text = result.text,
line = result.line,
path = vim.api.nvim_buf_get_name(0)
}
table.insert(commands, entry)
end
return commands
end
local function get_labels()
local query = "(label_definition (curly_group_text (text) @label_title))"
local labels = {}
local results = get_latex_element(query)
for _,result in pairs(results) do
local entry = {
text = result.text,
line = result.line,
path = vim.api.nvim_buf_get_name(0)
}
table.insert(labels, entry)
end
return labels
end
local function get_headings()
local headings = {}
local matches = {
'part',
'chapter',
'section',
'subsection',
'subsubsection',
'paragraph',
'subparagraph',
}
for _,type in pairs(matches) do
local results = get_latex_element("("..type.."(curly_group (text) @section))")
for _,result in pairs(results) do
local entry = {
type = type,
text = result.text,
line = result.line,
path = vim.api.nvim_buf_get_name(0)
}
table.insert(headings, entry)
end
end
return headings
end
local function get_frames()
local frames = {}
local frames_query ='(generic_environment (begin (curly_group_text (text) @frame (#eq? @frame "frame") generic_command (command_name) @command )))'
local results = get_latex_element(frames_query)
for _,result in pairs(results) do
local entry = {
type = type,
text = result.text,
line = result.line,
path = vim.api.nvim_buf_get_name(0)
}
table.insert(frames, entry)
end
return frames
end
local function telescope_newcommands(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = 'Select newcommand definition',
results_title = 'Newcommand',
finder = finders.new_table {
results = get_newcommands(),
entry_maker = function(entry)
return {
value = entry.text,
display = entry.text,
ordinal = entry.text,
filename = entry.path,
lnum = entry.line
}
end
},
previewer = conf.qflist_previewer(opts),
sorter = conf.file_sorter(opts),
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
local pattern = "{([^{}]+)}"
local resultTable = {}
for match in selection[1]:gmatch(pattern) do
table.insert(resultTable, match)
end
vim.api.nvim_put({ resultTable[2] }, "", false, true)
-- vim.api.nvim_put({ selection[2] }, "", false, true)
end)
return true
end,
})
:find()
end
local function telescope_labels(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = 'Select a label',
results_title = 'Labels',
finder = finders.new_table {
results = get_labels(),
entry_maker = function(entry)
return {
value = entry.text,
display = entry.text,
ordinal = entry.text,
filename = entry.path,
lnum = entry.line
}
end
},
previewer = conf.qflist_previewer(opts),
sorter = conf.file_sorter(opts),
}):find()
end
local function add_template_file(file)
local current_path = vim.fn.expand('%:p:h')
local current_file = vim.fn.expand('%:t')
local filename = file:match("^.+/(.+)$")
os.execute("cp "..file.." "..current_path.."/"..filename)
local pos = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_buf_set_lines(0, pos[1]-1, pos[1]-1, false, {"\\include{"..file.."}"})
end
local function telescope_templates(opts)
local temp_path = os.getenv("HOME").."/Phd/Templates"
local command = "fdfind -t f . '"..temp_path.."'"
local lines = {}
local file = io.popen(command)
for line in file:lines() do
table.insert(lines, line)
end
file:close()
pickers.new({}, {
prompt_title = "Templates",
finder = finders.new_table{ results = lines},
sorter = conf.generic_sorter(opts),
previewer = conf.qflist_previewer(opts),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
add_template_file(selection[1])
end)
return true
end,
}):find()
end
local function telescope_frames(opts)
local frames = get_frames()
pickers.new(opts, {
prompt_title = 'Select frame',
results_title = 'Frames',
finder = finders.new_table {
results = frames,
entry_maker = function(entry)
return {
value = entry,
display = entry.text,
ordinal = entry.text,
filename = entry.path,
lnum = entry.line
}
end
},
previewer = conf.qflist_previewer(opts),
sorter = conf.file_sorter(opts),
})
:find()
end
local function telescope_headings(opts)
local headings = get_headings()
pickers.new(opts, {
prompt_title = 'Select a heading',
results_title = 'Headings',
finder = finders.new_table {
results = headings,
entry_maker = function(entry)
return {
value = entry,
display = entry.type ..": "..entry.text,
ordinal = entry.type ..": "..entry.text,
filename = entry.path,
lnum = entry.line
}
end
},
previewer = conf.qflist_previewer(opts),
sorter = conf.file_sorter(opts),
})
:find()
end
return require("telescope").register_extension({
setup = function(ext_config, config)
-- Do not know how to use it.
end,
exports = {
headings = telescope_headings,
labels = telescope_labels,
newcommands = telescope_newcommands,
templates = telescope_templates,
frames = telescope_frames
},
})