Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/document.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ end
get_offset(doc, p::Position) = get_offset(doc, p.line, p.character)
get_offset(doc, r::Range) = get_offset(doc, r.start):get_offset(doc, r.stop)

# 1-based. Basically the index at which (line, character) can be found in the document.
function get_offset2(doc::Document, line::Integer, character::Integer, forgiving_mode=false)
line_offsets = get_line_offsets2!(doc)
text = get_text(doc)
Expand Down
2 changes: 1 addition & 1 deletion src/requests/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function julia_getModuleAt_request(params::VersionedTextDocumentPositionParams,
doc = getdocument(server, uri)
if doc._version == params.version
offset = get_offset2(doc, params.position.line, params.position.character, true)
x = get_expr(getcst(doc), offset)
x = get_expr_or_parent(getcst(doc), offset, 1)
if x isa EXPR
scope = StaticLint.retrieve_scope(x)
if scope !== nothing
Expand Down
26 changes: 26 additions & 0 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,32 @@ function get_expr(x, offset, pos=0, ignorewhitespace=false)
end
end

# like get_expr, but only returns a expr if offset is not on the edge of it's span
function get_expr_or_parent(x, offset, pos=0)
if pos > offset
return nothing
end
if length(x) > 0 && headof(x) !== :NONSTDIDENTIFIER
for a in x
if pos < offset <= (pos + a.fullspan)
if pos < offset < (pos + a.span)
return get_expr_or_parent(a, offset, pos)
else
return x
end
end
pos += a.fullspan
end
elseif pos == 0
return x
elseif (pos < offset <= (pos + x.fullspan))
if pos + x.span < offset
return x.parent
end
return x
end
end

function get_expr(x, offset::UnitRange{Int}, pos=0, ignorewhitespace=false)
if all(pos .> offset)
return nothing
Expand Down