diff --git a/scip_indexer/BUILD b/scip_indexer/BUILD index 13f515d02..b4499aabd 100644 --- a/scip_indexer/BUILD +++ b/scip_indexer/BUILD @@ -31,6 +31,8 @@ cc_library( "Debug.cc", "Debug.h", "SCIPIndexer.cc", + "SCIPProtoExt.cc", + "SCIPProtoExt.h", "SCIPSymbolRef.cc", "SCIPSymbolRef.h", ], diff --git a/scip_indexer/SCIPIndexer.cc b/scip_indexer/SCIPIndexer.cc index f6de9f48e..13cd9a0cd 100644 --- a/scip_indexer/SCIPIndexer.cc +++ b/scip_indexer/SCIPIndexer.cc @@ -33,6 +33,7 @@ #include "sorbet_version/sorbet_version.h" #include "scip_indexer/Debug.h" +#include "scip_indexer/SCIPProtoExt.h" #include "scip_indexer/SCIPSymbolRef.h" #include "scip_indexer/SCIPUtils.h" @@ -429,6 +430,8 @@ class SCIPState { for (auto &occurrence : occurrences->second) { *document.add_occurrences() = move(occurrence); } + fast_sort(*document.mutable_occurrences(), + [](const auto &o1, const auto &o2) -> bool { return scip::compareOccurrence(o1, o2) < 0; }); this->occurrenceMap.erase(file); } @@ -437,6 +440,9 @@ class SCIPState { for (auto &symbol : symbols->second) { *document.add_symbols() = move(symbol); } + fast_sort(*document.mutable_symbols(), [](const auto &s1, const auto &s2) -> bool { + return scip::compareSymbolInformation(s1, s2) < 0; + }); this->symbolMap.erase(file); } diff --git a/scip_indexer/SCIPProtoExt.cc b/scip_indexer/SCIPProtoExt.cc new file mode 100644 index 000000000..6e91fd218 --- /dev/null +++ b/scip_indexer/SCIPProtoExt.cc @@ -0,0 +1,71 @@ +#include "scip_indexer/SCIPProtoExt.h" + +#include + +namespace scip { +#define CHECK_CMP(expr) \ + { \ + auto cmp = expr; \ + if (cmp != 0) { \ + return cmp < 0; \ + } \ + } + +int compareDiagnostic(const scip::Diagnostic &d1, const scip::Diagnostic &d2) { + CHECK_CMP(int32_t(d1.severity()) - int32_t(d2.severity())); + CHECK_CMP(d1.code().compare(d2.code())); + CHECK_CMP(d1.message().compare(d2.message())); + CHECK_CMP(d1.source().compare(d2.source())); + CHECK_CMP(d1.tags_size() - d2.tags_size()); + for (auto i = 0; i < d1.tags_size(); i++) { + CHECK_CMP(int32_t(d1.tags(i)) - int32_t(d2.tags(i))); + } + return 0; +} + +int compareOccurrence(const scip::Occurrence &o1, const scip::Occurrence &o2) { + CHECK_CMP(o1.range_size() - o2.range_size()); + for (auto i = 0; i < o1.range_size(); i++) { + CHECK_CMP(o1.range(i) - o2.range(i)); + } + CHECK_CMP(o1.symbol().compare(o2.symbol())); + CHECK_CMP(o1.symbol_roles() - o2.symbol_roles()); + CHECK_CMP(o1.override_documentation_size() - o2.override_documentation_size()); + for (auto i = 0; i < o1.override_documentation_size(); i++) { + CHECK_CMP(o1.override_documentation(i).compare(o2.override_documentation(i))); + } + CHECK_CMP(int32_t(o1.syntax_kind()) - int32_t(o2.syntax_kind())); + CHECK_CMP(o1.diagnostics_size() - o2.diagnostics_size()); + for (auto i = 0; i < o1.diagnostics_size(); i++) { + CHECK_CMP(compareDiagnostic(o1.diagnostics(i), o2.diagnostics(i))); + } + return 0; +} + +int compareBool(bool b1, bool b2) { + return int8_t(b1) - int8_t(b2); +} + +int compareRelationship(const scip::Relationship &r1, const scip::Relationship &r2) { + CHECK_CMP(r1.symbol().compare(r2.symbol())); + CHECK_CMP(compareBool(r1.is_reference(), r2.is_reference())); + CHECK_CMP(compareBool(r1.is_implementation(), r2.is_implementation())); + CHECK_CMP(compareBool(r1.is_type_definition(), r2.is_type_definition())); + CHECK_CMP(compareBool(r1.is_definition(), r2.is_definition())); + return 0; +} + +int compareSymbolInformation(const scip::SymbolInformation &s1, const scip::SymbolInformation &s2) { + CHECK_CMP(s1.symbol().compare(s2.symbol())); + CHECK_CMP(s1.documentation_size() - s2.documentation_size()); + for (auto i = 0; i < s1.documentation_size(); i++) { + CHECK_CMP(s1.documentation(i).compare(s2.documentation(i))); + } + CHECK_CMP(s1.relationships_size() - s2.relationships_size()); + for (auto i = 0; i < s1.relationships_size(); i++) { + CHECK_CMP(compareRelationship(s1.relationships(i), s2.relationships(i))) + } + return 0; +} +#undef CHECK_CMP +} // namespace scip \ No newline at end of file diff --git a/scip_indexer/SCIPProtoExt.h b/scip_indexer/SCIPProtoExt.h new file mode 100644 index 000000000..8eaa03f93 --- /dev/null +++ b/scip_indexer/SCIPProtoExt.h @@ -0,0 +1,14 @@ +#ifndef SORBET_SCIP_PROTO_EXT +#define SORBET_SCIP_PROTO_EXT + +#include "proto/SCIP.pb.h" + +namespace scip { +int compareOccurrence(const scip::Occurrence &o1, const scip::Occurrence &o2); + +int compareRelationship(const scip::Relationship &r1, const scip::Relationship &r2); + +int compareSymbolInformation(const scip::SymbolInformation &s1, const scip::SymbolInformation &s2); +} // namespace scip + +#endif // SORBET_SCIP_PROTO_EXT \ No newline at end of file diff --git a/test/scip/testdata/blocks_lambdas_procs.snapshot.rb b/test/scip/testdata/blocks_lambdas_procs.snapshot.rb index 3ca47cf01..d0f281e37 100644 --- a/test/scip/testdata/blocks_lambdas_procs.snapshot.rb +++ b/test/scip/testdata/blocks_lambdas_procs.snapshot.rb @@ -92,8 +92,8 @@ def prc # ^^^ reference [..] ``#new(). # ^ definition local 3~#1283111692 y += x -# ^ reference local 1~#1283111692 # ^ reference (write) local 1~#1283111692 +# ^ reference local 1~#1283111692 # ^^^^^^ reference local 1~#1283111692 # ^ reference local 3~#1283111692 } diff --git a/test/scip/testdata/cattr.snapshot.rb b/test/scip/testdata/cattr.snapshot.rb index 1416e0ab9..10eb9fbc4 100644 --- a/test/scip/testdata/cattr.snapshot.rb +++ b/test/scip/testdata/cattr.snapshot.rb @@ -66,8 +66,8 @@ class CA # ^^^^^^ reference [..] Kernel#extend(). cattr_accessor :both, :foo # ^^^^^ definition [..] CA#`both=`(). -# ^^^^^ definition [..] ``#both(). # ^^^^^ definition [..] ``#`both=`(). +# ^^^^^ definition [..] ``#both(). # ^^^^^ definition [..] CA#both(). # ^^^^ definition [..] CA#foo(). # ^^^^ definition [..] ``#foo(). @@ -85,8 +85,8 @@ class CA # ^^^^ definition [..] ``#bar(). # ^^^^ definition [..] ``#`bar=`(). # ^^^^^^^^^^^^^^^^^^^ definition [..] ``#no_instance_writer(). -# ^^^^^^^^^^^^^^^^^^^ definition [..] ``#`no_instance_writer=`(). # ^^^^^^^^^^^^^^^^^^^ definition [..] CA#no_instance_writer(). +# ^^^^^^^^^^^^^^^^^^^ definition [..] ``#`no_instance_writer=`(). sig {void} def usages diff --git a/test/scip/testdata/def_delegator.snapshot.rb b/test/scip/testdata/def_delegator.snapshot.rb index cdd2bc0d9..5444c49a3 100644 --- a/test/scip/testdata/def_delegator.snapshot.rb +++ b/test/scip/testdata/def_delegator.snapshot.rb @@ -6,8 +6,8 @@ class MyArray1 # ^^^^^^^^ definition [..] MyArray1# attr_accessor :inner_array -# ^^^^^^^^^^^ definition [..] MyArray1#inner_array(). # ^^^^^^^^^^^ definition [..] MyArray1#`inner_array=`(). +# ^^^^^^^^^^^ definition [..] MyArray1#inner_array(). extend Forwardable # ^^^^^^ reference [..] Kernel#extend(). # ^^^^^^^^^^^ reference [..] Forwardable# @@ -54,8 +54,8 @@ class MyArray4 extend T::Sig # ^^^^^^ reference [..] Kernel#extend(). attr_accessor :inner_array -# ^^^^^^^^^^^ definition [..] MyArray4#inner_array(). # ^^^^^^^^^^^ definition [..] MyArray4#`inner_array=`(). +# ^^^^^^^^^^^ definition [..] MyArray4#inner_array(). extend Forwardable # ^^^^^^ reference [..] Kernel#extend(). # ^^^^^^^^^^^ reference [..] Forwardable# diff --git a/test/scip/testdata/encrypted_prop.snapshot.rb b/test/scip/testdata/encrypted_prop.snapshot.rb index 808bf4503..9ca820c54 100644 --- a/test/scip/testdata/encrypted_prop.snapshot.rb +++ b/test/scip/testdata/encrypted_prop.snapshot.rb @@ -26,14 +26,14 @@ def self.encrypted_prop(opts={}); end # ^^^^^^^^^^^^^^ definition [..] ``#encrypted_prop(). encrypted_prop :foo # ^^^^^^^^^^^^^^^^^^^ reference [..] String# +# ^^^ definition [..] EncryptedProp#encrypted_foo(). # ^^^ definition [..] EncryptedProp#`encrypted_foo=`(). # ^^^ definition [..] EncryptedProp#`foo=`(). -# ^^^ definition [..] EncryptedProp#encrypted_foo(). # ^^^ definition [..] EncryptedProp#foo(). encrypted_prop :bar, migrating: true, immutable: true # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference [..] String# -# ^^^ definition [..] EncryptedProp#bar(). # ^^^ definition [..] EncryptedProp#encrypted_bar(). +# ^^^ definition [..] EncryptedProp#bar(). end diff --git a/test/scip/testdata/fields_and_attrs.snapshot.rb b/test/scip/testdata/fields_and_attrs.snapshot.rb index ac6284252..1badd65c8 100644 --- a/test/scip/testdata/fields_and_attrs.snapshot.rb +++ b/test/scip/testdata/fields_and_attrs.snapshot.rb @@ -96,8 +96,8 @@ def m3 class P # ^ definition [..] P# attr_accessor :a -# ^ definition [..] P#`a=`(). # ^ definition [..] P#a(). +# ^ definition [..] P#`a=`(). attr_reader :r # ^ definition [..] P#r(). attr_writer :w diff --git a/test/scip/testdata/flatfile_dsl.snapshot.rb b/test/scip/testdata/flatfile_dsl.snapshot.rb index bd28fbec5..b8dc0624e 100644 --- a/test/scip/testdata/flatfile_dsl.snapshot.rb +++ b/test/scip/testdata/flatfile_dsl.snapshot.rb @@ -19,8 +19,8 @@ class Flatfile < Record # ^^^^^^^^ reference [..] ``#flatfile(). from 1..2, :foo # ^^^^ reference [..] ``#from(). -# ^^^^ definition [..] Flatfile#`foo=`(). # ^^^^ definition [..] Flatfile#foo(). +# ^^^^ definition [..] Flatfile#`foo=`(). pattern(/A-Za-z/, :bar) # ^^^^^^^ reference [..] ``#pattern(). # ^^^^^^^^ reference [..] Regexp# @@ -28,8 +28,8 @@ class Flatfile < Record # ^^^^ definition [..] Flatfile#bar(). field :baz # ^^^^^ reference [..] ``#field(). -# ^^^^ definition [..] Flatfile#baz(). # ^^^^ definition [..] Flatfile#`baz=`(). +# ^^^^ definition [..] Flatfile#baz(). end end diff --git a/test/scip/testdata/mattr.snapshot.rb b/test/scip/testdata/mattr.snapshot.rb index 34b52e36c..754657747 100644 --- a/test/scip/testdata/mattr.snapshot.rb +++ b/test/scip/testdata/mattr.snapshot.rb @@ -66,8 +66,8 @@ class MA # ^^^^^^ reference [..] Kernel#extend(). mattr_accessor :both, :foo # ^^^^^ definition [..] MA#`both=`(). -# ^^^^^ definition [..] ``#both(). # ^^^^^ definition [..] ``#`both=`(). +# ^^^^^ definition [..] ``#both(). # ^^^^^ definition [..] MA#both(). # ^^^^ definition [..] MA#foo(). # ^^^^ definition [..] ``#foo(). @@ -85,8 +85,8 @@ class MA # ^^^^ definition [..] ``#bar(). # ^^^^ definition [..] ``#`bar=`(). # ^^^^^^^^^^^^^^^^^^^ definition [..] ``#no_instance_writer(). -# ^^^^^^^^^^^^^^^^^^^ definition [..] ``#`no_instance_writer=`(). # ^^^^^^^^^^^^^^^^^^^ definition [..] MA#no_instance_writer(). +# ^^^^^^^^^^^^^^^^^^^ definition [..] ``#`no_instance_writer=`(). sig {void} def usages diff --git a/test/scip/testdata/minitest.snapshot.rb b/test/scip/testdata/minitest.snapshot.rb index 37e9a4b09..dfdada292 100644 --- a/test/scip/testdata/minitest.snapshot.rb +++ b/test/scip/testdata/minitest.snapshot.rb @@ -14,8 +14,8 @@ def outside_method # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition [..] MyTest#``(). CONST = 10 # ^^^^^ definition [..] MyTest#CONST. -# ^^^^^^^^^^ reference [..] Kernel#raise(). # ^^^^^^^^^^ reference [..] Kernel# +# ^^^^^^^^^^ reference [..] Kernel#raise(). end it "allows let-ed constants inside of IT" do diff --git a/test/scip/testdata/prop.snapshot.rb b/test/scip/testdata/prop.snapshot.rb index e6d114bf8..86db76333 100644 --- a/test/scip/testdata/prop.snapshot.rb +++ b/test/scip/testdata/prop.snapshot.rb @@ -8,8 +8,8 @@ class SomeODM # ^^^^^^^ reference [..] Module#include(). prop :foo, String -# ^^^ definition [..] SomeODM#`foo=`(). # ^^^ definition [..] SomeODM#foo(). +# ^^^ definition [..] SomeODM#`foo=`(). # ^^^^^^ reference [..] String# sig {returns(T.nilable(String))} @@ -70,54 +70,54 @@ class AdvancedODM # ^^^^^^ reference [..] String# prop :enum_prop, String, enum: ["hello", "goodbye"] -# ^^^^^^^^^ definition [..] AdvancedODM#enum_prop(). # ^^^^^^^^^ definition [..] AdvancedODM#`enum_prop=`(). +# ^^^^^^^^^ definition [..] AdvancedODM#enum_prop(). # ^^^^^^ reference [..] String# prop :foreign_lazy, String, foreign: -> {ForeignClass} # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#Boolean. # ^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_lazy_!`(). -# ^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_lazy_(). # ^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_lazy(). # ^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_lazy=`(). +# ^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_lazy_(). # ^^^^^^ reference [..] String# -# ^^ reference [..] Kernel# # ^^ reference [..] Kernel#lambda(). +# ^^ reference [..] Kernel# # ^^^^^^^^^^^^ reference [..] ForeignClass# prop :foreign_proc, String, foreign: proc {ForeignClass} # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#Boolean. -# ^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_proc_!`(). +# ^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_proc(). # ^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_proc_(). +# ^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_proc_!`(). # ^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_proc=`(). -# ^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_proc(). # ^^^^^^ reference [..] String# # ^^^^ reference [..] Kernel#proc(). # ^^^^^^^^^^^^ reference [..] ForeignClass# prop :foreign_invalid, String, foreign: proc { :not_a_type } # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference [..] T#Boolean. # ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_invalid_!`(). -# ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_invalid=`(). -# ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_invalid_(). # ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_invalid(). +# ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#foreign_invalid_(). +# ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#`foreign_invalid=`(). # ^^^^^^ reference [..] String# # ^^^^ reference [..] Kernel#proc(). prop :ifunset, String, ifunset: '' -# ^^^^^^^ definition [..] AdvancedODM#ifunset(). # ^^^^^^^ definition [..] AdvancedODM#`ifunset=`(). +# ^^^^^^^ definition [..] AdvancedODM#ifunset(). # ^^^^^^ reference [..] String# prop :ifunset_nilable, T.nilable(String), ifunset: '' -# ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#ifunset_nilable(). # ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#`ifunset_nilable=`(). +# ^^^^^^^^^^^^^^^ definition [..] AdvancedODM#ifunset_nilable(). # ^^^^^^ reference [..] String# prop :empty_hash_rules, String, {} -# ^^^^^^^^^^^^^^^^ definition [..] AdvancedODM#empty_hash_rules(). # ^^^^^^^^^^^^^^^^ definition [..] AdvancedODM#`empty_hash_rules=`(). +# ^^^^^^^^^^^^^^^^ definition [..] AdvancedODM#empty_hash_rules(). # ^^^^^^ reference [..] String# prop :hash_rules, String, { enum: ["hello", "goodbye" ] } -# ^^^^^^^^^^ definition [..] AdvancedODM#hash_rules(). # ^^^^^^^^^^ definition [..] AdvancedODM#`hash_rules=`(). +# ^^^^^^^^^^ definition [..] AdvancedODM#hash_rules(). # ^^^^^^ reference [..] String# end @@ -130,8 +130,8 @@ def self.token_prop(opts={}); end def self.created_prop(opts={}); end # ^^^^^^^^^^^^ definition [..] ``#created_prop(). token_prop -# ^^^^^ definition [..] PropHelpers#token(). # ^^^^^ definition [..] PropHelpers#`token=`(). +# ^^^^^ definition [..] PropHelpers#token(). # ^^^^^^^^^^ reference [..] ``#token_prop(). # ^^^^^^^^^^ reference [..] String# created_prop diff --git a/test/scip/testdata/struct.snapshot.rb b/test/scip/testdata/struct.snapshot.rb index 5c577768a..e75e5c725 100644 --- a/test/scip/testdata/struct.snapshot.rb +++ b/test/scip/testdata/struct.snapshot.rb @@ -2,13 +2,13 @@ # From Sorbet docs https://sorbet.org/docs/tstruct class S < T::Struct -# ^ definition [..] S#initialize(). # ^ definition [..] S# +# ^ definition [..] S#initialize(). # ^ reference [..] T# # ^^^^^^ definition [..] T#Struct# prop :prop_i, Integer -# ^^^^^^ definition [..] S#`prop_i=`(). # ^^^^^^ definition [..] S#prop_i(). +# ^^^^^^ definition [..] S#`prop_i=`(). # ^^^^^^^ reference [..] Integer# const :const_s, T.nilable(String) # ^^^^^^^ definition [..] S#const_s(). @@ -48,15 +48,15 @@ def f POINT = Struct.new(:x, :y) do #^^^^^ definition [..] POINT# -#^^^^^^^^^^^^^^^^^^^^ definition local 5~#119448696 #^^^^^^^^^^^^^^^^^^^^ definition [..] Struct# +#^^^^^^^^^^^^^^^^^^^^ definition local 5~#119448696 #^^^^^^^^^^^^^^^^^^^^ definition [..] POINT#initialize(). +# ^ reference [..] BasicObject# # ^ definition [..] POINT#x(). # ^ definition [..] POINT#`x=`(). -# ^ reference [..] BasicObject# +# ^ reference [..] BasicObject# # ^ definition [..] POINT#y(). # ^ definition [..] POINT#`y=`(). -# ^ reference [..] BasicObject# def array # ^^^^^ definition [..] POINT#array(). [x, y]