-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpep.py
4347 lines (3079 loc) · 119 KB
/
pep.py
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
import html
import inspect
import json
import os
import pathlib
import re
import shlex
import subprocess
import tempfile
import threading
import time
import traceback
from typing import List, Optional
from zipfile import ZipFile
import sublime # type: ignore
import sublime_plugin # type: ignore
from .src import progress
# Flags for creating/opening files in various ways.
# https://www.sublimetext.com/docs/api_reference.html#sublime.NewFileFlags
GOTO_DEFAULT_FLAGS = sublime.ENCODED_POSITION
GOTO_TRANSIENT_FLAGS = sublime.ENCODED_POSITION | sublime.TRANSIENT
GOTO_SIDE_BY_SIDE_FLAGS = (
sublime.ENCODED_POSITION
| sublime.SEMI_TRANSIENT
| sublime.ADD_TO_SELECTION
| sublime.CLEAR_TO_RIGHT
)
# Thingy types
TT_FINDING = "finding"
TT_KEYWORD = "keyword"
TT_SYMBOL = "symbol"
TT_LOCAL = "local"
TT_LOCAL_BINDING = "local_binding"
TT_LOCAL_USAGE = "local_usage"
TT_VAR_DEFINITION = "var_definition"
TT_VAR_USAGE = "var_usage"
TT_NAMESPACE_DEFINITION = "namespace_definition"
TT_NAMESPACE_USAGE = "namespace_usage"
TT_NAMESPACE_USAGE_ALIAS = "namespace_usage_alias"
TT_JAVA_CLASS_USAGE = "java_class_usage"
OUTPUT_PANEL_NAME = "pep"
OUTPUT_PANEL_NAME_PREFIXED = f"output.{OUTPUT_PANEL_NAME}"
HIGHLIGHTED_REGIONS_KEY = "pg_pep_highligths"
HIGHLIGHTED_STATUS_KEY = "pg_pep_highligths"
# Setting used to override the clj-kondo config for a view analysis.
SETTING_CLJ_KONDO_CONFIG = "pep_clj_kondo_config"
# Setting used to toggle a view's annotations.
SETTING_ANNOTATE_VIEW = "pep_annotate_view"
# Configuration shared by paths and view analysis - without a common configuration the index would be inconsistent.
CLJ_KONDO_VIEW_PATHS_ANALYSIS_CONFIG = "{:var-definitions true, :var-usages true, :arglists true, :locals true, :keywords true, :symbols true, :java-class-definitions false, :java-class-usages true, :java-member-definitions false, :instance-invocations true}"
CLJ_KONDO_CLASSPATH_ANALYSIS_CONFIG = "{:var-usages false :var-definitions {:shallow true} :arglists true :keywords true :java-class-definitions false}"
CLJ_KONDO_OUTPUT_JSON_CONFIG = "{:format :json :canonical-paths true}"
# Analysis reference: https://github.com/clj-kondo/clj-kondo/tree/master/analysis
CLJ_KONDO_VIEW_CONFIG = f"{{:analysis {CLJ_KONDO_VIEW_PATHS_ANALYSIS_CONFIG} :output {CLJ_KONDO_OUTPUT_JSON_CONFIG} }}"
CLJ_KONDO_PATHS_CONFIG = f"{{:skip-lint true :analysis {CLJ_KONDO_VIEW_PATHS_ANALYSIS_CONFIG} :output {CLJ_KONDO_OUTPUT_JSON_CONFIG} }}"
CLJ_KONDO_CLASSPATH_CONFIG = f"{{:skip-lint true :analysis {CLJ_KONDO_CLASSPATH_ANALYSIS_CONFIG} :output {CLJ_KONDO_OUTPUT_JSON_CONFIG} }}"
## -- Analysis Functions
def af_annotate(context, analysis):
"""
Analysis Function to annotate view.
Depends on setting to annotate view after analysis.
"""
if view := context["view"]:
if annotate_view_after_analysis(view.window()):
annotate_view(view)
def af_annotate_on_save(context, analysis):
"""
Analysis Function to annotate view on save.
Depends on setting to annotate on save.
"""
if view := context["view"]:
if annotate_view_on_save(view.window()):
annotate_view(view)
def af_highlight_thingy(context, analysis):
"""
Analysis Function to highlight Thingy under the cursor.
"""
if view := context["view"]:
if automatically_highlight(view.window()):
highlight_thingy(view)
def af_status_summary(context, analysis):
"""
Analysis Function to show findings summary in the status bar.
"""
if view := context["view"]:
view.run_command("pg_pep_view_summary_status")
# Default functions to run after analysis.
DEFAULT_VIEW_ANALYSIS_FUNCTIONS = [
af_annotate,
af_highlight_thingy,
af_status_summary,
]
# Mapping of filename to analysis data by semantic, e.g. var-definitions.
# (filename -> semantic -> list of 'thingies')
_index_ = {}
_view_analysis_ = {}
_classpath_analysis_ = {}
def project_index(project_path, not_found={}):
"""
Mapping of filename to analysis data by semantic, e.g. var-definitions.
"""
return _index_.get(project_path, not_found) if project_path else not_found
def update_project_index(project_path, index):
project_index_ = project_index(project_path)
global _index_
_index_[project_path] = {**project_index_, **index}
def clear_project_index(project_path):
global _index_
_index_.pop(project_path, None)
def clear_cache():
global _index_
_index_ = {}
global _view_analysis_
_view_analysis_ = {}
global _classpath_analysis_
_classpath_analysis_ = {}
def set_classpath_analysis(project_path, analysis):
"""
Updates analysis for project.
"""
global _classpath_analysis_
_classpath_analysis_[project_path] = analysis
def classpath_analysis(project_path, not_found={}):
"""
Returns analysis for project.
"""
global _classpath_analysis_
return _classpath_analysis_.get(project_path, not_found)
def set_view_analysis(view_id, analysis):
"""
Updates analysis for a particular view.
"""
global _view_analysis_
_view_analysis_[view_id] = analysis
def view_analysis(view_id, not_found={}):
"""
Returns analysis for a particular view.
"""
global _view_analysis_
return _view_analysis_.get(view_id, not_found)
def paths_analysis(project_path, not_found={}):
"""
Returns analysis for paths.
TODO: Comments
"""
project_index_ = project_index(project_path, not_found=not_found)
if not project_index_:
return not_found
analysis = unify_analysis(project_index_)
keyword_index_ = keyword_index(analysis)
namespace_index_ = namespace_index(
analysis,
nrn=False,
nrn_usages=False,
)
symbol_index_ = symbol_index(
analysis,
srn=False,
)
var_index_ = var_index(
analysis,
vrn=False,
vrn_usages=False,
)
java_class_index_ = java_class_index(
analysis,
jrn_usages=False,
)
return {
**keyword_index_,
**namespace_index_,
**symbol_index_,
**var_index_,
**java_class_index_,
}
# -- Settings
def settings():
return sublime.load_settings("Pep.sublime-settings")
def project_data(window) -> dict:
"""
Returns Pep's project data - it's always a dict.
Pep's project data is data about paths, classpath and settings.
"""
if window:
return window.project_data().get("pep", {}) if window.project_data() else {}
else:
return {}
def setting(window, k, not_found):
"""
Get setting k from project's data or Pep settings.
Returns not_found if setting k is is not set.
"""
v = project_data(window).get(k)
return v if v is not None else settings().get(k, not_found)
def is_debug(window):
return setting(window, "debug", False)
def analysis_applicable_to(window):
return setting(
window,
"analysis_applicable_to",
[
"Packages/Clojure/Clojure.sublime-syntax",
"Packages/Clojure/ClojureScript.sublime-syntax",
],
)
def analysis_delay(window):
return setting(window, "analysis_delay", 0.6)
def automatically_highlight(window):
return setting(window, "automatically_highlight", False)
def annotate_view_after_analysis(window):
return setting(window, "annotate_view_after_analysis", False)
def annotate_view_on_save(window):
return setting(window, "annotate_view_on_save", False)
def annotation_font_size(window):
return setting(window, "annotation_font_size", "0.9em")
def analyze_scratch_view(window):
return setting(window, "analyze_scratch_view", False)
# --- View Status Settings
def view_status_show_errors(window):
return setting(window, "view_status_show_errors", True)
def view_status_show_warnings(window):
return setting(window, "view_status_show_warnings", True)
def view_status_show_highlighted(window):
return setting(window, "view_status_show_highlighted", False)
def view_status_show_highlighted_prefix(window):
return setting(window, "view_status_show_highlighted_prefix", "")
def view_status_show_highlighted_suffix(window):
return setting(window, "view_status_show_highlighted_suffix", "")
# ---
def startupinfo():
# Hide the console window on Windows.
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return startupinfo
def clj_kondo_path(window):
return setting(window, "clj_kondo_path", None)
# -- Output Panel
def show_output_panel(window):
window.run_command("show_panel", {"panel": OUTPUT_PANEL_NAME_PREFIXED})
def hide_output_panel(window):
window.run_command("hide_panel", {"panel": OUTPUT_PANEL_NAME_PREFIXED})
def hide_active_output_panel(window):
if window.active_panel() == OUTPUT_PANEL_NAME_PREFIXED:
hide_output_panel(window)
def output_panel(window):
return window.find_output_panel(OUTPUT_PANEL_NAME) or window.create_output_panel(
OUTPUT_PANEL_NAME
)
# ---
def analysis_view_change_count(view):
return view_analysis(view.id()).get("view_change_count")
def analysis_findings(analysis):
return analysis.get("findings", {})
def analysis_summary(analysis):
return analysis.get("summary", {})
def analysis_kindex(analysis):
"""
Returns a dictionary of keywords by (namespace, name).
'kindex' stands for 'keyword index'.
"""
return analysis.get("kindex", {})
def analysis_sindex(analysis):
"""
Returns a dictionary of symbols by symbol.
'sindex' stands for 'symbol index'.
"""
return analysis.get("sindex", {})
def analysis_krn(analysis):
"""
Returns a dictionary of keywords by row.
This index can be used to quicky find a keyword by row.
'krn' stands for 'keyword row name'.
"""
return analysis.get("krn", {})
def analysis_vindex(analysis):
"""
Returns a dictionary of vars by (namespace, name).
'vindex' stands for 'var index'.
"""
return analysis.get("vindex", {})
def analysis_vindex_usages(analysis):
"""
Returns a dictionary of Var usages by (namespace, name).
'vindex_usages' stands for 'Var index'.
"""
return analysis.get("vindex_usages", {})
def analysis_vrn(analysis):
"""
Returns a dictionary of Vars by row.
This index can be used to quicky find a Var definition by row.
'vrn' stands for 'var row name'.
"""
return analysis.get("vrn", {})
def analysis_vrn_usages(analysis):
"""
Returns a dictionary of Var usages by row.
This index can be used to quicky find a Var usage by row.
'vrn' stands for 'var row name'.
"""
return analysis.get("vrn_usages", {})
def analysis_jindex(analysis):
"""
Returns a dictionary of Java class definitions indexed by name.
'jindex' stands for 'java class definition index'.
"""
return analysis.get("jindex", {})
def analysis_jrn_usages(analysis):
"""
Returns a dictionary of Java class usages by row.
This index can be used to quicky find a Java class usage by row.
'jrn' stands for 'java row name'.
"""
return analysis.get("jrn_usages", {})
def analysis_jindex_usages(analysis):
"""
Returns a dictionary of Java class usages indexed by name - Class name to a set of class usages.
'jindex_usages' stands for 'java class usages index'.
"""
return analysis.get("jindex_usages", {})
def analysis_lindex(analysis):
"""
Returns a dictionary of locals by ID.
This index can be used to find a local in constant time if you know its ID.
When finding usages from a usage itself, the first step is to find the usage,
once you have found it, you can use its ID to find the local.
Locals and usages have the same ID,
so it's possible to corretale a usage with a local.
'lindex' stands for 'local index'.
"""
return analysis.get("lindex", {})
def analysis_lrn(analysis):
"""
Returns a dictionary of locals by row.
This index can be used to quicky find a local definition by row.
Example: (let [a| 1] ...)
'lrn' stands for 'local row name'.
"""
return analysis.get("lrn", {})
def analysis_lrn_usages(analysis):
"""
Returns a dictionary of local usages by row.
This index can be used to quicky find a local usage by row.
Example: (let [a 1] |a)
'lrn' stands for 'local row name'.
"""
return analysis.get("lrn_usages", {})
def analysis_nindex(analysis):
"""
Returns a dictionary of namespace definition by name.
'nindex' stands for 'Namespace index'.
"""
return analysis.get("nindex", {})
def analysis_nindex_usages(analysis):
"""
Returns a dictionary of namespace usages by name.
'nindex' stands for 'namespace index'.
"""
return analysis.get("nindex_usages", {})
def analysis_nrn(analysis):
"""
Returns a dictionary of namespaces by row.
"""
return analysis.get("nrn", {})
def analysis_nrn_usages(analysis):
"""
Returns a dictionary of namespace usages by row.
This index can be used to quicky find a namespace usage by row.
'nrn' stands for 'namespace row name'.
"""
return analysis.get("nrn_usages", {})
# ---
def namespace_definitions(analysis):
"""
Returns a list of namespace definitions.
"""
l = []
for namespace_definitions in analysis_nindex(analysis).values():
l.extend(namespace_definitions)
return l
def namespace_usages(analysis):
"""
Returns a list of namespace usages.
"""
l = []
for namespace_usages in analysis_nindex_usages(analysis).values():
l.extend(namespace_usages)
return l
def var_definitions(analysis):
"""
Returns a list of var definitions.
"""
l = []
for var_definitions in analysis_vindex(analysis).values():
l.extend(var_definitions)
return l
def var_usages(analysis):
"""
Returns a list of var_usage.
"""
l = []
for var_usages in analysis_vindex_usages(analysis).values():
l.extend(var_usages)
return l
def keyword_regs(analysis) -> List:
"""
Returns a list of keyword where reg is not None.
"""
l = []
for keywords_ in analysis_kindex(analysis).values():
for keyword_ in keywords_:
if keyword_.get("reg"):
l.append(keyword_)
return l
def recursive_usage(thingy_usage):
usage_from = thingy_usage.get("from")
usage_to = thingy_usage.get("to")
usage_name = thingy_usage.get("name")
usage_from_var = thingy_usage.get("from-var")
is_same_ns = usage_from == usage_to
is_same_var = usage_name == usage_from_var
return is_same_ns and is_same_var
# ---
def namespace_index(
analysis,
nindex=True,
nindex_usages=True,
nrn=True,
nrn_usages=True,
):
"""
Index namespace definitions and usages.
Definitions are indexed by name and file extension.
Usages are indexed by name.
Returns dict with keys 'nindex', 'nindex_usages', 'nrn', 'nrn_usages'.
"""
namespace_definitions = analysis.get("namespace-definitions", [])
# Namespace definitions indexed by name.
nindex_ = {}
# Namespace definitions indexed by row.
nrn_ = {}
if nindex or nrn:
for namespace_definition in namespace_definitions:
# Ignore data missing row and col - it seems like a clj-kondo bug.
if namespace_definition.get("row") and namespace_definition.get("col"):
namespace_definition = {
**namespace_definition,
"_semantic": TT_NAMESPACE_DEFINITION,
}
if nindex:
name = namespace_definition.get("name")
nindex_.setdefault(name, []).append(namespace_definition)
if nrn:
name_row = namespace_definition.get("name-row")
nrn_.setdefault(name_row, []).append(namespace_definition)
# Namespace usages indexed by name.
nindex_usages_ = {}
# Var usages indexed by row.
nrn_usages_ = {}
if nindex_usages or nrn_usages:
for namespace_usage in analysis.get("namespace-usages", []):
# Ignore data missing row and col - it seems like a clj-kondo bug.
if namespace_usage.get("row") and namespace_usage.get("col"):
namespace_usage = {
**namespace_usage,
"_semantic": TT_NAMESPACE_USAGE,
}
if nindex_usages:
name = namespace_usage.get("to")
nindex_usages_.setdefault(name, []).append(namespace_usage)
if nrn_usages:
name_row = namespace_usage.get("name-row")
nrn_usages_.setdefault(name_row, []).append(namespace_usage)
# Index alias row (in case there's one).
# Note: It's possible to have both the name and alias in the same row.
if namespace_usage.get("alias"):
alias_row = namespace_usage.get("alias-row")
nrn_usages_.setdefault(alias_row, []).append(namespace_usage)
return {
"nindex": nindex_,
"nindex_usages": nindex_usages_,
"nrn": nrn_,
"nrn_usages": nrn_usages_,
}
def local_index(
analysis,
lindex=True,
lindex_usages=True,
lrn=True,
lrn_usages=True,
):
"""
Index local definitions and usages.
Definitions and usages are indexed by id.
Returns dict with keys 'lindex', 'lindex_usages', 'lrn', 'lrn_usages'.
"""
# Locals indexed by row.
lrn_ = {}
# Locals indexed by ID.
lindex_ = {}
if lindex or lrn:
for local_binding in analysis.get("locals", []):
# Ignore data missing row and col - it seems like a clj-kondo bug.
if local_binding.get("row") and local_binding.get("col"):
local_binding = {
**local_binding,
"_semantic": TT_LOCAL,
}
id = local_binding.get("id")
row = local_binding.get("row")
if lrn:
lrn_.setdefault(row, []).append(local_binding)
if lindex:
lindex_[id] = local_binding
# Local usages indexed by ID - local binding ID to a set of local usages.
lindex_usages_ = {}
# Local usages indexed by row.
lrn_usages_ = {}
if lindex_usages or lrn_usages:
for local_usage in analysis.get("local-usages", []):
# Ignore data missing row and col - it seems like a clj-kondo bug.
if local_usage.get("row") and local_usage.get("col"):
local_usage = {
**local_usage,
"_semantic": TT_LOCAL_USAGE,
}
id = local_usage.get("id")
name_row = local_usage.get("name-row")
if lindex_usages:
lindex_usages_.setdefault(id, []).append(local_usage)
if lrn_usages:
lrn_usages_.setdefault(name_row, []).append(local_usage)
return {
"lindex": lindex_,
"lindex_usages": lindex_usages_,
"lrn": lrn_,
"lrn_usages": lrn_usages_,
}
def keyword_index(
analysis,
kindex=True,
krn=True,
):
# Keywords indexed by name - tuple of namespace and name.
kindex_ = {}
# Keywords indexed by row.
krn_ = {}
if kindex or krn:
for keyword in analysis.get("keywords", []):
# Ignore data missing row and col - it seems like a clj-kondo bug.
if keyword.get("row") and keyword.get("col"):
keyword = {
**keyword,
"_semantic": TT_KEYWORD,
}
ns = keyword.get("ns")
name = keyword.get("name")
row = keyword.get("row")
if kindex:
kindex_.setdefault((ns, name), []).append(keyword)
if krn:
krn_.setdefault(row, []).append(keyword)
return {
"kindex": kindex_,
"krn": krn_,
}
def var_index(
analysis,
vindex=True,
vindex_usages=True,
vrn=True,
vrn_usages=True,
):
# Vars indexed by row.
vrn_ = {}
# Vars indexed by namespace and name.
vindex_ = {}
if vindex or vrn:
for var_definition in analysis.get("var-definitions", []):
# Ignore data missing row and col - it seems like a clj-kondo bug.
if var_definition.get("row") and var_definition.get("col"):
var_definition = {
**var_definition,
"_semantic": TT_VAR_DEFINITION,
}
if vindex:
ns = var_definition.get("ns")
name = var_definition.get("name")
vindex_.setdefault((ns, name), []).append(var_definition)
if vrn:
name_row = var_definition.get("name-row")
vrn_.setdefault(name_row, []).append(var_definition)
# Var usages indexed by row.
vrn_usages_ = {}
# Var usages indexed by name - var name to a set of var usages.
vindex_usages_ = {}
if vindex_usages or vrn_usages:
for var_usage in analysis.get("var-usages", []):
# Ignore data missing row and col - it seems like a clj-kondo bug.
if var_usage.get("row") and var_usage.get("col"):
var_usage = {
**var_usage,
"_semantic": TT_VAR_USAGE,
}
if vindex_usages:
ns = var_usage.get("to")
name = var_usage.get("name")
vindex_usages_.setdefault((ns, name), []).append(var_usage)
if vrn_usages:
name_row = var_usage.get("name-row")
vrn_usages_.setdefault(name_row, []).append(var_usage)
return {
"vindex": vindex_,
"vrn": vrn_,
"vindex_usages": vindex_usages_,
"vrn_usages": vrn_usages_,
}
def symbol_index(
analysis,
sindex=True,
srn=True,
):
# Symbols indexed by row.
srn_ = {}
# Symbols indexed by symbol.
sindex_ = {}
if sindex or srn:
for sym in analysis.get("symbols", []):
# Ignore data missing row and col.
if sym.get("row") and sym.get("col"):
sym = {
**sym,
"_semantic": TT_SYMBOL,
}
if sindex:
sindex_.setdefault(sym.get("symbol"), []).append(sym)
if srn:
srn_.setdefault(sym.get("row"), []).append(sym)
return {
"sindex": sindex_,
"srn": srn_,
}
def java_class_index(
analysis,
jindex=True,
jindex_usages=True,
jrn_usages=True,
):
"""
Index Java class definitions and usages.
Definitions and usages are indexed by class name.
Returns dict with keys 'jindex', 'jindex_usages', 'jrn_usages'.
"""
# Java class definition indexed by class name.
jindex_ = {}
if jindex:
for java_class_definition in analysis.get("java-class-definitions", []):
if java_class_definition.get("row") and java_class_definition.get("col"):
jindex_[java_class_definition.get("class")] = java_class_definition
# Java class usages indexed by row.
jrn_usages_ = {}
# Java class usages indexed by name - Class name to a set of class usages.
jindex_usages_ = {}
if jindex_usages or jrn_usages:
for java_class_usage in analysis.get("java-class-usages", []):
if java_class_usage.get("row") and java_class_usage.get("col"):
java_class_usage = {
**java_class_usage,
"_semantic": TT_JAVA_CLASS_USAGE,
}
if jindex_usages:
jindex_usages_.setdefault(java_class_usage.get("class"), []).append(
java_class_usage
)
if jrn_usages:
jrn_usages_.setdefault(java_class_usage.get("row"), []).append(
java_class_usage
)
return {
"jindex": jindex_,
"jindex_usages": jindex_usages_,
"jrn_usages": jrn_usages_,
}
# ---
def file_extension(filename):
if filename:
return pathlib.Path(filename).suffix