-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
1170 lines (1067 loc) · 32.8 KB
/
init.lua
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- mod-version:3
--
-- Source Control Management plugin.
-- @copyright Jefferson Gonzalez <[email protected]>
-- @license MIT
--
-- Note: Some ideas and bits taken from:
-- https://github.com/vincens2005/lite-xl-gitdiff-highlight
-- https://github.com/lite-xl/lite-xl-plugins/blob/master/plugins/gitstatus.lua
-- Thanks to everyone involved!
--
local core = require "core"
local common = require "core.common"
local command = require "core.command"
local config = require "core.config"
local keymap = require "core.keymap"
local style = require "core.style"
local util = require "plugins.scm.util"
local changes = require "plugins.scm.changes"
local Doc = require "core.doc"
local DocView = require "core.docview"
local StatusView = require "core.statusview"
local ReadDoc = require "plugins.scm.readdoc"
local Git = require "plugins.scm.backend.git"
local Fossil = require "plugins.scm.backend.fossil"
local MessageBox = require "libraries.widget.messagebox"
---Backends shipped with the plugin.
---@type table<string,plugins.scm.backend>
local BACKENDS
---@class config.plugins.smc
---@field highlighter boolean
---@field highlighter_alignment "right" | "left"
---@field git_path string
---@field fossil_path string
config.plugins.smc = common.merge({
highlighter = true,
highlighter_alignment = "right",
git_path = "git",
fossil_path = "fossil",
config_spec = {
name = "Source Control Management",
{
label = "Highlighter",
description = "Display or hide the changes highlighter from the gutter.",
path = "highlighter",
type = "toggle",
default = true
},
{
label = "Highlighter Alignment",
description = "The position on the gutter to draw the changes highlighter.",
path = "highlighter_alignment",
type = "selection",
default = "left",
values = {
{"Left", "left"},
{"Right", "right"}
}
},
{
label = "Git Path",
description = "Path to the Git binary.",
path = "git_path",
type = "FILE",
default = "git",
filters = {"git$", "git%.exe$"},
on_apply = function(value)
if not BACKENDS.Git:set_command(value) then
BACKENDS.Git:set_command(common.basename(value))
end
end
},
{
label = "Fossil Path",
description = "Path to the Fossil binary.",
path = "fossil_path",
type = "FILE",
default = "fossil",
filters = {"fossil$", "fossil%.exe$"},
on_apply = function(value)
if not BACKENDS.Fossil:set_command(value) then
BACKENDS.Fossil:set_command(common.basename(value))
end
end
}
}
}, config.plugins.smc)
-- initialize backends
BACKENDS = { Git = Git(), Fossil = Fossil() }
BACKENDS.Git:set_command(config.plugins.smc.git_path)
BACKENDS.Fossil:set_command(config.plugins.smc.fossil_path)
---@class plugins.scm.filechange : plugins.scm.backend.filechange
---@field color renderer.color?
---@field text string?
---@class plugins.scm
local scm = {}
---Show the blame information of active line.
---@type boolean
scm.show_blame = false
---List of loaded projects current branch.
---@type table<string, string>
local BRANCHES = {}
---List of loaded projects current stats.
---@type table<string, plugins.scm.backend.stats>
local STATS = {}
---List of loaded project changes.
---@type table<string,table<string,plugins.scm.filechange>>
local CHANGES = {}
---Opened projects SCM backends list
---@type table<string,plugins.scm.backend>
local PROJECTS = {}
setmetatable(PROJECTS, {
__index = function(t, k)
if k == nil then return nil end
local v = rawget(t, k)
if v == nil then
for _, backend in pairs(BACKENDS) do
if backend:detect(k) then
v = backend
backend:get_branch(k, function(branch)
BRANCHES[k] = branch
backend:get_stats(k, function(stats) STATS[k] = stats end)
end)
if backend.name == "Fossil" then
table.insert(config.ignore_files, "%-shm$")
table.insert(config.ignore_files, "%-wal$")
end
rawset(t, k, v)
end
end
end
return v
end
})
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
---@param doc core.doc
local function update_doc_diff(doc)
if doc.abs_filename then
local project_dir = util.get_file_project_dir(doc.abs_filename)
if project_dir and PROJECTS[project_dir] then
local backend = PROJECTS[project_dir]
backend:get_file_diff(doc.abs_filename, project_dir, function(diff)
if diff and diff ~= "" then
local parsed_diff = changes.parse(diff)
doc.scm_diff = nil
for _, _ in pairs(parsed_diff) do
doc.scm_diff = parsed_diff
break
end
else
doc.scm_diff = nil
end
end)
return
end
end
doc.scm_diff = nil
end
---@param doc core.doc
local function update_doc_blame(doc)
if not scm.show_blame then
if doc.blame_list then doc.blame_list = nil end
return
end
if doc.abs_filename then
local project_dir = util.get_file_project_dir(doc.abs_filename)
if project_dir and PROJECTS[project_dir] then
local backend = PROJECTS[project_dir]
backend:get_file_blame(doc.abs_filename, project_dir, function(list)
if list and #list > 0 then
doc.blame_list = list
else
doc.blame_list = nil
end
end)
return
end
end
if doc.blame_list then doc.blame_list = nil end
end
---@param path string
---@param nonblocking? boolean
local function update_doc_status(path, nonblocking)
local project_dir = util.get_file_project_dir(path)
local backend = PROJECTS[project_dir]
if backend then
if not nonblocking then backend:set_blocking_mode(true) end
backend:get_file_status(path, project_dir, function(status)
if status and status ~= "" then
local color
if status == "added" then
color = style.good
elseif status == "edited" then
color = style.warn
elseif status == "renamed" then
color = style.warn
elseif status == "deleted" then
color = style.error
elseif status == "untracked" then
color = style.dim
end
if color then
if not CHANGES[project_dir] then CHANGES[project_dir] = {} end
CHANGES[project_dir][path] = {
path = path,
color = color,
status = status
}
else
if CHANGES[project_dir] and CHANGES[project_dir][path] then
CHANGES[project_dir][path] = nil
end
end
end
end)
if not nonblocking then backend:set_blocking_mode(true) end
end
end
--------------------------------------------------------------------------------
-- Source Control Management API
--------------------------------------------------------------------------------
---Get a file branch or current project branch if no file given.
---@param abs_filename? string
---@return string?
function scm.get_branch(abs_filename)
local project = util.get_project_dir(abs_filename)
return BRANCHES[project]
end
---Get current project insert and delete stats.
---@return plugins.scm.backend.stats?
function scm.get_stats()
local project = util.get_current_project()
return STATS[project]
end
---Get current project scm backend
---@return plugins.scm.backend?
function scm.get_backend()
local project = util.get_current_project()
return PROJECTS[project]
end
---@param path string
---@param is_changed? boolean Only get backend if file has changed
---@param is_tracked? boolean Only get backend if file is tracked
---@return plugins.scm.backend?
function scm.get_path_backend(path, is_changed, is_tracked)
local project_dir = util.get_project_dir(path)
if project_dir then
if is_changed then
if CHANGES[project_dir] and CHANGES[project_dir][path] then
if is_tracked then
if
CHANGES[project_dir][path].status
and
CHANGES[project_dir][path].status == "untracked"
then
return nil
end
end
return PROJECTS[project_dir]
end
else
local backend = PROJECTS[project_dir]
if is_tracked and backend then
---@type plugins.scm.backend.filestatus
local status
backend:set_blocking_mode(true)
backend:get_file_status(path, project_dir, function(file_status)
status = file_status
end)
backend:set_blocking_mode(false)
if status == "untracked" then return nil end
end
return backend
end
end
return nil
end
---@return plugins.scm.backend.filestatus
function scm.get_path_status(path)
local backend = scm.get_path_backend(path)
local project_dir = util.get_project_dir(path)
if backend and project_dir then
local status
backend:set_blocking_mode(true)
backend:get_file_status(path, project_dir, function(file_status)
status = file_status
end)
backend:set_blocking_mode(false)
return status
end
return "untracked"
end
---@return plugins.scm.filechange?
function scm.get_path_changes(path)
local project_dir = util.get_project_dir(path)
if CHANGES[project_dir] and CHANGES[project_dir][path] then
return CHANGES[project_dir] and CHANGES[project_dir][path]
end
return nil
end
---@return boolean
function scm.is_staged(path)
local backend = scm.get_path_backend(path)
local project_dir = util.get_project_dir(path)
if backend and project_dir then
if CHANGES[project_dir] and CHANGES[project_dir][path] then
if
CHANGES[project_dir][path].path
and
CHANGES[project_dir][path].new_path
and
not system.get_file_info(CHANGES[project_dir][path].path)
and
system.get_file_info(CHANGES[project_dir][path].new_path)
then
path = CHANGES[project_dir][path].new_path
else
return CHANGES[project_dir][path].staged
end
end
local staged_files
local path_rel = common.relative_path(project_dir, path)
backend:set_blocking_mode(true)
backend:get_staged(project_dir, function(files)
staged_files = files
end)
backend:set_blocking_mode(false)
if staged_files[path_rel] then return true end
end
return false
end
---Check if the given project path is source control managed.
---@param path string
---@return boolean
function scm.is_scm_project(path)
for _, project in ipairs(core.project_directories) do
if path == project.name and PROJECTS[path] then
return true
end
end
return false
end
---Add a new SCM backend.
---@param backend plugins.scm.backend
function scm.register_backend(backend)
BACKENDS[backend.name] = backend
end
---Remove an existing SCM backend.
---@param name string
function scm.unregister_backend(name)
BACKENDS[name] = nil
end
---@param project_dir? string
function scm.open_diff(project_dir)
project_dir = project_dir or util.get_current_project()
local backend = PROJECTS[project_dir]
if backend then
backend:get_diff(project_dir, function(diff)
if diff and diff ~= "" then
local title = "[CHANGES].diff"
---@type plugins.scm.readdoc
local diffdoc = ReadDoc(title, title)
diffdoc:set_text(diff)
core.root_view:open_doc(diffdoc)
else
core.warn("SCM: no changes detected.")
end
end)
else
core.warn("SCM: current project directory is not versioned.")
end
end
function scm.open_path_diff(path)
local project_dir = util.get_project_dir(path)
local backend = PROJECTS[project_dir]
if backend then
local path_rel = common.relative_path(project_dir, path)
backend:get_file_diff(path, project_dir, function(diff)
if diff and diff ~= "" then
local title = string.format("%s.diff", path_rel)
---@type plugins.scm.readdoc
local diffdoc = ReadDoc(title, title)
diffdoc:set_text(diff)
core.root_view:open_doc(diffdoc)
else
local info = system.get_file_info(path)
if info and info.type == "file" then
core.warn("SCM: seems like the file is untracked.")
else
core.warn("SCM: seems like the path only contains untracked files.")
end
end
end)
end
end
function scm.open_commit_diff(commit, project_dir)
local backend = PROJECTS[project_dir]
if backend then
core.log("SCM: generating the diff please wait...")
backend:get_commit_diff(commit, project_dir, function(diff)
if diff and diff ~= "" then
local title = string.format("[%s].diff", commit)
---@type plugins.scm.readdoc
local diffdoc = ReadDoc(title, title)
diffdoc:set_text(diff)
core.root_view:open_doc(diffdoc)
else
core.warn("SCM: could not retrieve the commit diff.")
end
end)
end
end
---@param project_dir? string
function scm.open_project_status(project_dir)
project_dir = project_dir or util.get_current_project()
local backend = PROJECTS[project_dir]
if backend then
backend:get_status(project_dir, function(status)
if status and status ~= "" then
local title = "Project Status"
---@type plugins.scm.readdoc
local doc = ReadDoc(title, title)
doc:set_text(status)
core.root_view:open_doc(doc)
else
core.warn("SCM: no status to report.")
end
end)
else
core.warn("SCM: current project directory is not versioned.")
end
end
---@param project_dir string
function scm.pull(project_dir)
local backend = PROJECTS[project_dir]
if backend then
backend:pull(project_dir, function(success, errmsg)
if success then
core.log("SCM: pulled latest changes for '%s'", project_dir)
else
core.error("SCM: failed to pull '%s', %s", project_dir, errmsg)
end
end)
end
end
---@param path string
function scm.revert_file(path)
local project_dir = util.get_project_dir(path)
local backend = PROJECTS[project_dir]
if project_dir and backend then
local path_rel = common.relative_path(project_dir, path)
MessageBox.warning(
"SCM Restore File",
{
"Do you really want to revert local changes?\n\n",
"File: " .. path_rel
},
function(_, button_id)
if button_id == 1 then
backend:revert_file(path, project_dir, function(success, errmsg)
if success then
core.log("SCM: file '%s' changes reverted", path_rel)
update_doc_status(path)
util.reload_doc(path)
else
core.error("SCM: failed reverting '%s', %s", path_rel, errmsg)
end
end)
end
end,
MessageBox.BUTTONS_YES_NO
)
end
end
---@param path string
function scm.add_path(path)
local project_dir = util.get_project_dir(path)
local backend = PROJECTS[project_dir]
if project_dir and backend then
local path_rel = common.relative_path(project_dir, path)
backend:add_path(path, project_dir, function(success, errmsg)
if success then
core.log("SCM: file '%s' added", path_rel)
update_doc_status(path)
else
core.error("SCM: failed adding '%s', %s", path_rel, errmsg)
end
end)
end
end
---@param path string
function scm.remove_path(path)
local project_dir = util.get_project_dir(path)
local backend = PROJECTS[project_dir]
if project_dir and backend then
local path_rel = common.relative_path(project_dir, path)
backend:remove_path(path, project_dir, function(success, errmsg)
if success then
core.log("SCM: file '%s' removed", path_rel)
update_doc_status(path)
else
core.error("SCM: failed removing '%s', %s", path_rel, errmsg)
end
end)
end
end
---@field from string
---@field to string
---@field callback fun(oldname:string, newname:string):any
function scm.move_path(from, to, callback)
local project_dir = util.get_project_dir(from)
local backend = PROJECTS[project_dir]
local moved = false
if
backend and common.path_belongs_to(from, project_dir)
and
common.path_belongs_to(to, project_dir)
then
local from_rel = common.relative_path(project_dir, from)
local to_rel = common.relative_path(project_dir, to)
backend:set_blocking_mode(true)
backend:move_path(from, to, project_dir, function(success, errmsg)
if success then
core.log("SCM: file '%s' moved to '%s'", from_rel, to_rel)
update_doc_status(to, true)
else
core.error(
"SCM: failed moving '%s' to '%s' with: %s",
from_rel, to_rel, errmsg
)
end
end)
backend:set_blocking_mode(false)
end
if system.get_file_info(to) then
return true
end
return callback(from, to)
end
---@param path string
function scm.stage_file(path)
local project_dir = util.get_project_dir(path)
local backend = PROJECTS[project_dir]
if project_dir and backend and backend:has_staging() then
local path_rel = common.relative_path(project_dir, path)
backend:stage_file(path, project_dir, function(success, errmsg)
if success then
core.log("SCM: file '%s' staged", path_rel)
update_doc_status(path)
else
core.error("SCM: failed staging '%s', %s", path_rel, errmsg)
end
end)
end
end
---@param path string
function scm.unstage_file(path)
local project_dir = util.get_project_dir(path)
local backend = PROJECTS[project_dir]
if project_dir and backend and backend:has_staging() then
local path_rel = common.relative_path(project_dir, path)
backend:unstage_file(path, project_dir, function(success, errmsg)
if success then
core.log("SCM: file '%s' unstaged", path_rel)
update_doc_status(path)
else
core.error("SCM: failed unstaging '%s', %s", path_rel, errmsg)
end
end)
end
end
---Go to next change in a file.
---@param doc? core.doc
function scm.next_change(doc)
doc = doc or util.get_current_doc()
if not doc or not doc.scm_diff then return end
local line, col = doc:get_selection()
while doc.scm_diff[line] do
line = line + 1
end
while line < #doc.lines do
if doc.scm_diff[line] then
doc:set_selection(line, col, line, col)
return
end
line = line + 1
end
end
---Go to previous change in a file.
---@param doc? core.doc
function scm.previous_change(doc)
doc = doc or util.get_current_doc()
if not doc or not doc.scm_diff then return end
local line, col = doc:get_selection()
while doc.scm_diff[line] do
line = line - 1
end
while line > 0 do
if doc.scm_diff[line] then
doc:set_selection(line, col, line, col)
return
end
line = line - 1
end
end
---Update the SCM status of all open projects.
function scm.update()
for project_dir, project_backend in pairs(PROJECTS) do
project_backend:get_branch(project_dir, function(branch, cached)
if not cached then BRANCHES[project_dir] = branch end
project_backend:get_stats(project_dir, function(stats, cached)
if not cached then STATS[project_dir] = stats end
project_backend:get_changes(project_dir, function(filechanges, cached)
if cached then return end
local changed_files = {}
for i, change in ipairs(filechanges) do
local color = style.modified
if change.status == "added" then
color = style.good
elseif change.status == "edited" then
color = style.warn
elseif change.status == "renamed" then
color = style.warn
elseif change.status == "deleted" then
color = style.error
elseif change.status == "untracked" then
color = style.dim
end
change.color = color
local path = ""
if change.new_path then
change.text = common.basename(change.path)
.. " -> "
.. common.basename(change.new_path)
changed_files[change.new_path] = change
path = common.dirname(change.new_path)
else
changed_files[change.path] = change
path = common.dirname(change.path)
end
while path do
if #path < #project_dir then break end
changed_files[path] = { color = style.modified }
path = common.dirname(path)
end
if i % 10 == 0 then
coroutine.yield()
end
end
CHANGES[project_dir] = changed_files
end)
end)
end)
end
end
--------------------------------------------------------------------------------
-- Keep the project branch, changes and stats updated
--------------------------------------------------------------------------------
core.add_thread(function()
while true do
scm.update()
coroutine.yield(1)
end
end)
--------------------------------------------------------------------------------
-- Override Doc to register diff changes and blame history
--------------------------------------------------------------------------------
local doc_save = Doc.save
function Doc:save(...)
doc_save(self, ...)
update_doc_diff(self)
update_doc_blame(self)
end
local doc_new = Doc.new
function Doc:new(...)
doc_new(self, ...)
update_doc_diff(self)
update_doc_blame(self)
end
local doc_load = Doc.load
function Doc:load(...)
doc_load(self, ...)
update_doc_diff(self)
update_doc_blame(self)
end
local doc_raw_insert = Doc.raw_insert
function Doc:raw_insert(line, col, text, undo_stack, time)
doc_raw_insert(self, line, col, text, undo_stack, time)
local diffs = self.scm_diff or {}
if diffs[line] ~= "addition" then
diffs[line] = "modification"
end
local count = line
for _ in (text .. "\n"):gmatch("(.-)\n") do
if count ~= line then
diffs[count] = "addition"
end
count = count + 1
end
self.scm_diff = diffs
end
local doc_raw_remove = Doc.raw_remove
function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time)
doc_raw_remove(self, line1, col1, line2, col2, undo_stack, time)
local diffs = self.scm_diff or {}
if line1 ~= line2 then
local minline = math.min(line1, line2)
local maxline = math.max(line1, line2)
for line = minline+1, maxline do
diffs[line] = "deletion"
end
else
diffs[line1] = "modification"
end
self.scm_diff = diffs
end
--------------------------------------------------------------------------------
-- Override DocView to draw changes on gutter and blame tooltip
--------------------------------------------------------------------------------
local DIFF_WIDTH = 3
local docview_draw_line_gutter = DocView.draw_line_gutter
local docview_get_gutter_width = DocView.get_gutter_width
function DocView:draw_line_gutter(line, x, y, width)
if not self.doc or not self.doc.scm_diff or not config.plugins.smc.highlighter then
return docview_draw_line_gutter(self, line, x, y, width)
end
local lh = self:get_line_height()
local gw, gpad = docview_get_gutter_width(self)
local diff_type = self.doc.scm_diff[line]
local align = config.plugins.smc.highlighter_alignment
if align == "right" then
docview_draw_line_gutter(self, line, x, y, gpad and gw - gpad or gw)
else
local tox = style.padding.x * DIFF_WIDTH / 12
docview_draw_line_gutter(self, line, x + tox, y, gpad and gw - gpad or gw)
end
if diff_type == nil then return end
local color = style.good
if diff_type == "deletion" then
color = style.error
elseif diff_type == "modification" then
color = style.warn
end
local colw = self:get_font():get_width(#self.doc.lines)
-- add margin in between highlight and text
if align == "right" then
if colw + style.padding.x * 2 >= gw then
x = x + style.padding.x * 1.5 + colw
else
x = x + gw - style.padding.x * 2 + (style.padding.x * DIFF_WIDTH / 12)
end
else
local spacing = (style.padding.x * DIFF_WIDTH / 12)
if colw + style.padding.x * 2 >= gw then
x = x + gw + spacing - colw - gpad
else
x = x + math.max(gw, colw) - (colw) - math.min(colw, gw) - spacing
end
end
local yoffset = self:get_line_text_y_offset()
if diff_type ~= "deletion" then
renderer.draw_rect(x, y + yoffset, DIFF_WIDTH, self:get_line_height(), color)
return
end
renderer.draw_rect(x - DIFF_WIDTH * 2, y + yoffset, DIFF_WIDTH * 4, 2, color)
return lh
end
function DocView:get_gutter_width()
if not self.doc or not self.doc.scm_diff or not config.plugins.smc.highlighter then
return docview_get_gutter_width(self)
end
return docview_get_gutter_width(self)
+ style.padding.x * DIFF_WIDTH / 12
end
local function draw_tooltip(text, x, y)
local font = style.font
local lh = font:get_height()
local ty = y + lh + (2 * style.padding.y)
local width = 0
local lines = {}
for line in string.gmatch(text.."\n", "(.-)\n") do
width = math.max(width, font:get_width(line))
table.insert(lines, line)
end
y = y + lh + style.padding.y
local height = #lines * font:get_height()
renderer.draw_rect(
x, y,
width + style.padding.x * 2, height + style.padding.y * 2,
style.background3
)
for _, line in pairs(lines) do
common.draw_text(
font, style.text, line, "left",
x + style.padding.x, ty,
width, lh
)
ty = ty + lh
end
end
local docview_draw = DocView.draw
function DocView:draw()
docview_draw(self)
if not self.doc or not scm.get_backend() or not self.doc.blame_list then
return
end
local line = self.doc:get_selection()
local info = self.doc.blame_list[line]
if info then
local x, y = self:get_line_screen_position(line)
local backend = scm.get_path_backend(self.doc.abs_filename)
if backend then
local text
if not info.text then
text = string.format(
"%s Blame | %s | (%s) %s",
backend.name, info.commit, info.author, info.date
)
end
draw_tooltip(info.text or text, x, y)
if not info.text and not info.getting then
info.getting = true
backend:get_commit_info(
info.commit,
util.get_project_dir(self.doc.abs_filename) or "",
function(commit)
local message = commit.summary or ""
if commit.message then
message = message .. "\n\n" .. commit.message
end
info.text = string.format(
"%s Blame | %s | (%s) %s | %s",
backend.name, info.commit, info.author, info.date, message
)
end
)
end
end
end
end
--------------------------------------------------------------------------------
-- Override rename to execute it on the SCM
--------------------------------------------------------------------------------
local os_rename = os.rename
function os.rename(oldname, newname)
return scm.move_path(oldname, newname, os_rename)
end
--------------------------------------------------------------------------------
-- StatusBar Item to show current branch and stats
--------------------------------------------------------------------------------
local scm_status_item = core.status_view:add_item({
name = "status:scm",
alignment = StatusView.Item.RIGHT,
get_item = function()
local project = util.get_current_project()
if
not PROJECTS[project]
or
not BRANCHES[project] or not STATS[project]
then
return {}
end
local bcolor = (STATS[project].inserts ~= 0 or STATS[project].deletes ~= 0)
and style.accent or style.text
local icolor = STATS[project].inserts ~= 0 and style.accent or style.text
local dcolor = STATS[project].deletes ~= 0 and style.accent or style.text
return {
bcolor, BRANCHES[project],
style.dim, " ",
icolor, "+", STATS[project].inserts,
style.dim, " / ",
dcolor, "-", STATS[project].deletes,
}
end,
position = -1,
tooltip = "current branch",
separator = core.status_view.separator2
})
scm_status_item.on_click = function(button)
if button == "right" then
command.perform "scm:global-diff"
else
core.command_view:set_text("Scm: ")
command.perform "core:find-command"
end
end
--------------------------------------------------------------------------------
-- Commands
--------------------------------------------------------------------------------
command.add(
function()
local valid = false
local project_dir = nil
local av = core.active_view
if av and av.doc and av.doc.abs_filename then
project_dir = util.get_project_dir(av.doc.abs_filename)
if project_dir and PROJECTS[project_dir] then valid = true end
end
if not valid and PROJECTS[core.project_dir] then
valid, project_dir = true, core.project_dir
end
return valid, project_dir
end, {
["scm:global-diff"] = function(project_dir)
scm.open_diff(project_dir)
end,
["scm:project-status"] = function(project_dir)
scm.open_project_status(project_dir)
end
})
command.add(nil, {
["scm:toggle-blame"] = function()
scm.show_blame = not scm.show_blame
for _, doc in ipairs(core.docs) do
update_doc_blame(doc)
end