Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb65b06c

Browse files
committed
feat(picker): consolidate all diff options underopts.previewers.diff. Default style isfancy
1 parent803bbea commitb65b06c

File tree

2 files changed

+73
-34
lines changed

2 files changed

+73
-34
lines changed

‎lua/snacks/picker/config/defaults.lua‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local M = {}
22

33
---@aliassnacks.picker.format.resolvefun(max_width:number):snacks.picker.Highlight[]
44
---@aliassnacks.picker.Extmarkvim.api.keyset.set_extmark|{col:number,row?:number,field?:string}
5-
---@aliassnacks.picker.Text{[1]:string,[2]:(string|string[])?,virtual?:boolean,field?:string,resolve?:snacks.picker.format.resolve,meta?:table<string,any>}
5+
---@aliassnacks.picker.Text{[1]:string,[2]:(string|string[])?,virtual?:boolean,field?:string,resolve?:snacks.picker.format.resolve,meta?:table<string,any>,inline?:boolean}
66
---@aliassnacks.picker.Highlightsnacks.picker.Text|snacks.picker.Extmark
77
---@aliassnacks.picker.formatfun(item:snacks.picker.Item,picker:snacks.Picker):snacks.picker.Highlight[]
88
---@aliassnacks.picker.previewfun(ctx:snacks.picker.preview.ctx):boolean?
@@ -177,11 +177,19 @@ local defaults = {
177177
---@classsnacks.picker.previewers.Config
178178
previewers= {
179179
diff= {
180-
builtin=true,-- use Neovim for previewing diffs (true) or use an external tool (false)
181-
cmd= {"delta"},-- example to show a diff with delta
180+
-- fancy: Snacks fancy diff (borders, multi-column line numbers, syntax highlighting)
181+
-- syntax: Neovim's built-in diff syntax highlighting
182+
-- terminal: external command (git's pager for git commands, `cmd` for other diffs)
183+
style="fancy",---@type"fancy"|"syntax"|"terminal"
184+
cmd= {"delta"},-- example for using `delta` as the external diff command
185+
---@typevim.wo?|{}window options for the diff preview window
186+
wo= {
187+
breakindent=true,
188+
wrap=true,
189+
linebreak=true,
190+
},
182191
},
183192
git= {
184-
builtin=true,-- use Neovim for previewing git output (true) or use git (false)
185193
args= {},-- additional arguments passed to the git command. Useful to set pager options usin `-c ...`
186194
},
187195
file= {

‎lua/snacks/picker/preview.lua‎

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ function M.file(ctx)
181181
ctx.preview:loc()
182182
end
183183

184+
---@paramdiffstring|string[]|snacks.picker.diff.Block[]
185+
---@paramft"diff"|"git"
186+
---@paramctxsnacks.picker.preview.ctx
187+
localfunctionfancy_diff(diff,ft,ctx)
188+
require("snacks.picker.util.diff").render(ctx.preview:scratch(),ns,diff, {ft=ft })
189+
Snacks.util.wo(ctx.win,ctx.picker.opts.previewers.diff.woor {})
190+
ifft~="diff"then
191+
ctx.preview:highlight({ft=ft })
192+
end
193+
end
194+
184195
---@paramcmdstring[]
185196
---@paramctxsnacks.picker.preview.ctx
186197
---@paramopts?snacks.job.Opts|{ft?:string}
@@ -190,48 +201,55 @@ function M.cmd(cmd, ctx, opts)
190201
localbuf=ctx.preview:scratch()
191202
vim.bo[buf].buftype="nofile"
192203

193-
localjob=Job.new(
194-
buf,
195-
cmd,
196-
Snacks.config.merge(opts, {
197-
debug=ctx.picker.opts.debug.proc,
198-
term=opts.term~=falseandnotopts.ftandopts.pty~=false,
199-
width=vim.api.nvim_win_get_width(ctx.win),
200-
height=vim.api.nvim_win_get_height(ctx.win),
201-
cwd=ctx.item.cwdorctx.picker.opts.cwd,
202-
env= {
203-
PAGER="cat",
204-
DELTA_PAGER="cat",
205-
},
206-
})
207-
)
204+
opts=Snacks.config.merge(opts, {
205+
debug=ctx.picker.opts.debug.proc,
206+
term=opts.term~=falseandnotopts.ftandopts.pty~=false,
207+
width=vim.api.nvim_win_get_width(ctx.win),
208+
height=vim.api.nvim_win_get_height(ctx.win),
209+
cwd=ctx.item.cwdorctx.picker.opts.cwd,
210+
env= {
211+
PAGER="cat",
212+
DELTA_PAGER="cat",
213+
},
214+
})
208215

209-
ifopts.ftthen
216+
localstyle=ctx.picker.opts.previewers.diff.style
217+
ifstyle=="fancy"andvim.tbl_contains({"diff","git"},opts.ft)then
218+
opts.on_line=function()endornil-- disable default line handler
219+
opts.on_lines=function(_,lines)
220+
fancy_diff(lines,opts.ft,ctx)
221+
end
222+
end
223+
224+
localjob=Job.new(buf,cmd,opts)
225+
226+
ifopts.ftandstyle~="fancy"then
210227
ctx.preview:highlight({ft=opts.ft })
211228
end
212229
returnjob
213230
end
214231

215232
---@paramctxsnacks.picker.preview.ctx
233+
---@returnstring[],boolean terminal
216234
localfunctiongit(ctx, ...)
217-
localbuiltin=ctx.picker.opts.previewers.git.builtin
218-
localret= {"git","-c","delta."..vim.o.background.."=true"}
219-
vim.list_extend(ret,builtinand {"--no-pager"}or {})
235+
localterminal=ctx.picker.opts.previewers.diff.style=="terminal"
236+
localret= {"git"}
237+
vim.list_extend(ret,notterminaland {"--no-pager"}or {})
220238
vim.list_extend(ret,ctx.picker.opts.previewers.git.argsor {})
221239
vim.list_extend(ret, {... })
222-
returnret,builtin
240+
returnret,terminal
223241
end
224242

225243
---@paramctxsnacks.picker.preview.ctx
226244
functionM.git_show(ctx)
227-
localcmd,builtin=git(ctx,"show",ctx.item.commit)
245+
localcmd,terminal=git(ctx,"show",ctx.item.commit)
228246
localpathspec=ctx.item.filesorctx.item.file
229247
pathspec=type(pathspec)=="table"andpathspecor {pathspec }
230248
if#pathspec>0then
231249
cmd[#cmd+1]="--"
232250
vim.list_extend(cmd,pathspec)
233251
end
234-
M.cmd(cmd,ctx, {ft=builtinand"git"ornil })
252+
M.cmd(cmd,ctx, {ft=notterminaland"git"ornil })
235253
end
236254

237255
---@paramctxsnacks.picker.preview.ctx
@@ -265,20 +283,24 @@ function M.git_log(ctx)
265283
author=author,
266284
},ctx.picker)
267285
Snacks.picker.highlight.render(ctx.buf,ns, {hl }, {append=true })
286+
Snacks.util.wo(ctx.win, {breakindent=true,wrap=true,linebreak=true })
268287
end
269288
end,
270289
})
271290
end
272291

273292
---@paramctxsnacks.picker.preview.ctx
274293
functionM.diff(ctx)
275-
localbuiltin=ctx.picker.opts.previewers.diff.builtin
276-
ifbuiltinthen
294+
localstyle=ctx.picker.opts.previewers.diff.style
295+
localcmd=vim.deepcopy(ctx.picker.opts.previewers.diff.cmd)
296+
style=style=="terminal"andvim.fn.executable(cmd[1])==0and"fancy"orstyle
297+
ifstyle=="syntax"then
277298
ctx.item.preview= {text=ctx.item.diff,ft="diff",loc=false }
278299
returnM.preview(ctx)
300+
elseifstyle~="terminal"then
301+
returnfancy_diff(ctx.item.diff,"diff",ctx)
279302
end
280-
localcmd=vim.deepcopy(ctx.picker.opts.previewers.diff.cmd)
281-
ifcmd[1]=="delta"then
303+
ifcmd[1]=="delta"andnotvim.tbl_contains(cmd,"--dark")andnotvim.tbl_contains(cmd,"--light")then
282304
table.insert(cmd,2,"--"..vim.o.background)
283305
end
284306
M.cmd(cmd,ctx, {
@@ -288,17 +310,26 @@ end
288310

289311
---@paramctxsnacks.picker.preview.ctx
290312
functionM.git_diff(ctx)
291-
localcmd,builtin=git(ctx,"diff","HEAD")
313+
localcmd,terminal=git(ctx,"diff","--no-ext-diff")
314+
ifnotctx.item.statusthen
315+
cmd[#cmd+1]="HEAD"-- generic diff against HEAD
316+
elseifctx.item.status:find("[UAD][UAD]")then
317+
cmd[#cmd+1]="--cc"-- combined diff for conflicts
318+
elseifctx.item.status:sub(1,1)~=""then
319+
cmd[#cmd+1]="--cached"-- staged changes
320+
end
292321
ifctx.item.filethen
293322
vim.list_extend(cmd, {"--",ctx.item.file })
294323
end
295-
M.cmd(cmd,ctx, {ft=builtinand"diff"ornil })
324+
M.cmd(cmd,ctx, {
325+
ft=notterminaland"diff"ornil,
326+
})
296327
end
297328

298329
---@paramctxsnacks.picker.preview.ctx
299330
functionM.git_stash(ctx)
300-
localcmd,builtin=git(ctx,"stash","show","--patch",ctx.item.stash)
301-
M.cmd(cmd,ctx, {ft=builtinand"diff"ornil })
331+
localcmd,terminal=git(ctx,"stash","show","--patch",ctx.item.stash)
332+
M.cmd(cmd,ctx, {ft=notterminaland"diff"ornil })
302333
end
303334

304335
---@paramctxsnacks.picker.preview.ctx

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp