m-demare / hlargs.nvim

Highlight arguments' definitions and usages, using Treesitter
GNU General Public License v3.0
472 stars 10 forks source link

Support for julia #41

Closed cnrrobertson closed 2 years ago

cnrrobertson commented 2 years ago

Language support request

Test file:

function standard(x)
  print(x)
end

function default(a,b="test")
  print(b)
end

function keyword(a,b="test";c)
  print(c)
end

function default_kw(a,b="test";c="keyword")
  print(c)
end

function nested1(x)
  function nested2(x)
    print(x)
  end
  return nested2(x)
end

function dispatch(x::Int,y::Any)
  println(typeof(x))
  println(typeof(y))
  return x
end

function typed_output(x::Int,y::Float64)::Int
  println(typeof(x))
  println(typeof(y))
  return x
end

function parametric(x::T) where {T<:Real}
  print(x)
end

inline(x) = print(x)

struct StructConstructor
   x::Real
   y::Real
   StructConstructor(x,y) = x > y ? error("out of order") : new(x,y)
end

# Overload operator
import Base.+
(+)(x::Int,y::Float64) = x*y
Base.:+(x::Int,y::Float64) = x*y

# Anonymous functions
x -> 3*x
(x) -> 3*x
z = (x,y) -> 3*x*y
# Can be defined anywhere
map(x->x*2 + 1, [1,2,3])
# Equivalent to do-block
map([1,2,3]) do x
  x*2 + 1
end

slurp(x...) = print(x)
m-demare commented 2 years ago

Should be (mostly) working now. The julia parser has some issues, probably because of the ability to override operators Mainly, the following lines aren't properly parsed:

import Base.+
(+)(x::Int,y::Float64) = x*y
Base.:+(x::Int,y::Float64) = x*y

Other than that, it should be working, let me know if you find any issues!

cnrrobertson commented 2 years ago

Amazing! Thank you for the very quick response.

I've noticed a couple little edge cases it's not quite working in that I should have mentioned.

One simple case:

function standard(x)
  print(x)
  x += 11
  x /= 11
  x *= 11
  x -= 11
end

The highlighting doesn't show up on the x += 11,x /= 11 etc.

Another case (from some real code):

function run_simulation!(prob,equation,vars,params,grid,nsteps,tjump,sjump,ld0,mu0)
    an.set_ic!(prob, ld0, mu0)
    ld = zeros(eltype(vars.ld),size(vars.ld)...)
    mu = zeros(eltype(vars.ld),size(vars.mu)...)
    ldh = zeros(eltype(vars.ldh),size(vars.ldh)...)
    muh = zeros(eltype(vars.ldh),size(vars.muh)...)
    copyto!(ld, vars.ld)
    copyto!(mu, vars.mu)
    copyto!(ldh, prob.sol[:,:,1])
    copyto!(muh, prob.sol[:,:,2])
    kr = 1:length(grid.kr)
    l = 1:length(grid.l)
    Dlta = @. sqrt(ld^2+mu^2)
    S = @. 2*Dlta
    Ev1 = @. (ld + Dlta) / sqrt((ld+Dlta)^2 + mu^2)
    Ev2 = @. mu / sqrt((ld+Dlta)^2 + mu^2)
    xout, yout, tpe = vec.(mxcall(:DefectLoc6a, 3, ld0, mu0))
end

I'm not sure what's happening here. The highlighting for the variables works on the first line, doesn't work on lines 2-5, works on lines 6-9, doesn't for the rest. I hope there is something helpful there.. Is this possibly just an issue with the treesitter parser?

m-demare commented 2 years ago

Thanks for the report! What happened is that the parser treats some function definitions the same way it treats assignments (for example, the StructConstructor in the original sample file). I got around this, but I didn't realize that identifiers used in an assignment statement weren't being recognized as such. Should be working now

cnrrobertson commented 2 years ago

It looks great now. Thanks for getting it all ship shape so quickly!