Skip to content

Commit

Permalink
Ranking: standardize ctags kind names before scoring (#674)
Browse files Browse the repository at this point in the history
SCIP ctags can output different kind names than universal-ctags (for example
`typeAlias` instead of `talias`). This change makes sure we handle different
names for the same kind. To do so, it refactors the logic so we first match
strings to standard kinds, then decide how these are scored for each language.
That way, you don't need to remember to cover all the possible kind names each
time you adjust scoring for a new language.

Also added basic tests for Ruby and Python to ensure we don't accidentally
change the scoring.
  • Loading branch information
jtibshirani authored and keegancsmith committed Nov 1, 2023
1 parent 39e647c commit ebf3aed
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 87 deletions.
64 changes: 64 additions & 0 deletions build/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,16 @@ func TestScoring(t *testing.T) {
t.Fatal(err)
}

examplePython, err := os.ReadFile("./testdata/example.py")
if err != nil {
t.Fatal(err)
}

exampleRuby, err := os.ReadFile("./testdata/example.rb")
if err != nil {
t.Fatal(err)
}

exampleScala, err := os.ReadFile("./testdata/example.scala")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1077,6 +1087,60 @@ func Get() {
wantScore: 8110,
},
//
// Python
//
{
fileName: "example.py",
content: examplePython,
query: &query.Substring{Content: true, Pattern: "C1"},
wantLanguage: "Python",
// 7000 (symbol) + 1000 (Python class) + 500 (word) + 10 (file order)
wantScore: 8510,
},
{
fileName: "example.py",
content: examplePython,
query: &query.Substring{Content: true, Pattern: "g"},
wantLanguage: "Python",
// 7000 (symbol) + 800 (Python function) + 500 (word) + 10 (file order)
wantScore: 8310,
},
{
fileName: "example.py",
content: examplePython,
query: &query.Substring{Content: true, Pattern: "__init__"},
wantLanguage: "Python",
// 7000 (symbol) + 400 (Python member) + 50 (partial word) + 10 (file order)
wantScore: 7460,
},
//
// Ruby
//
{
fileName: "example.rb",
content: exampleRuby,
query: &query.Substring{Content: true, Pattern: "Parental"},
wantLanguage: "Ruby",
// 7000 (symbol) + 1000 (Ruby class) + 500 (word) + 10 (file order)
wantScore: 8510,
},
{
fileName: "example.rb",
content: exampleRuby,
query: &query.Substring{Content: true, Pattern: "parental_func"},
wantLanguage: "Ruby",
// 7000 (symbol) + 900 (Ruby method) + 500 (word) + 10 (file order)
wantScore: 8410,
},
{
fileName: "example.rb",
content: exampleRuby,
query: &query.Substring{Content: true, Pattern: "MyModule"},
wantLanguage: "Ruby",
// 7000 (symbol) + 500 (Ruby module) + 500 (word) + 10 (file order)
wantScore: 8210,
},
//
// Scala
//
{
Expand Down
94 changes: 94 additions & 0 deletions build/testdata/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# v py.f def
# v py.f.x def
def f(x):

# v py.f.g def
def g():
y = 5

if True:
# v py.f.x ref
y = x # < "y" py.f.y def
else:
l1 = 3 # < "l1" py.f.l1 def

# v py.f.i def
for i in range(10):
# v py.f.i ref
l2 = i # < "l2" py.f.l2 def

while False:
l3 = 3 # < "l3" py.f.l3 def

try:
l4 = 3 # < "l4" py.f.l4 def
# v py.f.e def
except Exception as e:
l5 = 3 # < "l5" py.f.l5 def
# v py.f.e ref
_ = e

# vvvv py.f.file def
with open("file.txt") as file:
# vvvv py.f.file fef
print(file)

# vvv py.f.lam def
# vvv py.f.lam ref
_ = lambda lam: lam

# v py.f.y ref
# vv py.f.l1 ref
# vv py.f.l2 ref
# vv py.f.l3 ref
# vv py.f.l4 ref
# vv py.f.l5 ref
# v py.f.g ref
_ = y + l1 + l2 + l3 + l4 + l5 + g()

# vvv recursive.foo ref,nodef
recursive = recursive.foo


# vv py.C1 def
class C1:
x = 5 # < "x" py.C1.x def

def __init__(self, y):
# v py.C1.y def
self.y = y

def f(self):
# v py.C1.x ref
# v py.C1.g ref
self.x = self.g()

# v py.C1.g def
def g(self):
# v py.C1.y ref
return self.y


class C2(C1):
y = C1()

def f(self, c1: C1):
c = c1
# v py.C1.g ref
# v py.C1.x ref
return self.g() + c.x


def newC1() -> C1:
return C1()


# v py.C1.x ref
_ = newC1().x

# v py.C1.x ref
# v py.C1.x ref
_ = C1().x + C2().y.x

if False:
f(3) # < "f" py.f ref
77 changes: 77 additions & 0 deletions build/testdata/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
SOME_CONSTANT = 2.718

if true
a = 1
elsif false
b = 2
else
c = 3
end

(1..5).each do |counter|
z = 3
end

for counter in 1..5
y = 10
end

counter = 1
while counter <= 5 do
no = true
counter += 1
end

begin
raise NoMemoryError, 'Z.'
rescue NoMemoryError => exception_variable
puts 'A', exception_variable
rescue RuntimeError => other_exception_variable
puts 'K'
else
puts 'L'
ensure
puts 'O'
end

grade = 42
case grade
when 0.100
shouldntgetcaptured = true
puts 'you got a grade i guess'
end

module MyModule
def self.abc(base)
end

class MyClass
def yay
end

def self.woo(base)
end
end
end

class Foo
attr_accessor :bar
attr_reader :baz
attr_writer :qux
end

class Aliased
def bar
end

alias_method :baz, :bar
end

class Parental
def parental_func()
end
end

class Composed
include Parental
end
Loading

0 comments on commit ebf3aed

Please sign in to comment.