Skip to content

Commit 90bfee4

Browse files
committed
Move position functions to utils
1 parent 6fbc420 commit 90bfee4

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

src/JETLS.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ using .LSP
1212
include("JSONRPC.jl")
1313
using .JSONRPC
1414

15-
include("utils.jl")
16-
1715
using Pkg, JuliaSyntax, JET
1816

1917
struct FileInfo
@@ -74,6 +72,7 @@ function ServerState(send::F) where F
7472
Ref{String}())
7573
end
7674

75+
include("utils.jl")
7776
include("completions.jl")
7877

7978
struct IncludeCallback <: Function
@@ -149,7 +148,6 @@ function handle_message(state::ServerState, msg)
149148
elseif msg isa DocumentDiagnosticRequest || msg isa WorkspaceDiagnosticRequest
150149
@assert false
151150
elseif msg isa CompletionRequest
152-
@info "CompletionRequest" msg
153151
try
154152
return handle_CompletionRequest(state, msg)
155153
catch e

src/completions.jl

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,9 @@
11
import JuliaLowering: SyntaxTree, JuliaLowering as JL
22
import JuliaSyntax: @K_str, @KSet_str, JuliaSyntax as JS
33

4-
"""
5-
Fetch cached FileInfo given an LSclient-provided structure with a URI
6-
"""
7-
function get_fileinfo(s::ServerState, t::TextDocumentIdentifier)
8-
uri = URI(t.uri)
9-
return haskey(s.file_cache, uri) ? s.file_cache[uri] : nothing
10-
end
114
# get_fileinfo(s::ServerState, t::TextDocumentPositionParams) = get_fileinfo(s, t.textDocument)
125
get_fileinfo(s::ServerState, t::CompletionParams) = get_fileinfo(s, t.textDocument)
136

14-
# JuliaLowering uses byte offsets; LSP uses lineno and UTF-* character offset.
15-
# These functions do the conversion.
16-
17-
"""
18-
Convert 0-based `(;line = y, character = x)` to a 1-based byte offset
19-
"""
20-
function xy_to_offset(fi::FileInfo, pos::Position)
21-
code = fi.parsed_stream.textbuf
22-
b = 0
23-
for z in 1:pos.line
24-
b = findnext(isequal(UInt8('\n')), code, b + 1)
25-
end
26-
lend = findnext(isequal(UInt8('\n')), code, b + 1)
27-
lend = isnothing(lend) ? lastindex(code) + 1 : lend
28-
s = String(code[b+1:lend-1]) # current line, containing no newlines
29-
line_b = 1
30-
for i in 1:pos.character
31-
line_b = nextind(s, line_b)
32-
end
33-
return b + line_b
34-
end
35-
36-
"""
37-
Convert a 1-based byte offset to a 0-based line and character number
38-
"""
39-
function offset_to_xy(fi::FileInfo, b::Integer)
40-
sf = JuliaSyntax.SourceFile(fi.parsed_stream)
41-
l, c = JuliaSyntax.source_location(sf, b)
42-
return Position(;line = l-1, character = c-1)
43-
end
44-
457
"""
468
Like `Base.unique`, but over node ids, and with this comment promising that the
479
lowest-index copy of each node is kept.

src/utils.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,42 @@ let throttled = Dict{UInt, Tuple{Union{Nothing,Timer}, Float64}}()
3838
nothing
3939
end
4040
end
41+
42+
"""
43+
Fetch cached FileInfo given an LSclient-provided structure with a URI
44+
"""
45+
function get_fileinfo(s::ServerState, t::TextDocumentIdentifier)
46+
uri = URI(t.uri)
47+
return haskey(s.file_cache, uri) ? s.file_cache[uri] : nothing
48+
end
49+
50+
# JuliaLowering uses byte offsets; LSP uses lineno and UTF-* character offset.
51+
# These functions do the conversion.
52+
53+
"""
54+
Convert 0-based `(;line = y, character = x)` to a 1-based byte offset
55+
"""
56+
function xy_to_offset(fi::FileInfo, pos::Position)
57+
code = fi.parsed_stream.textbuf
58+
b = 0
59+
for z in 1:pos.line
60+
b = findnext(isequal(UInt8('\n')), code, b + 1)
61+
end
62+
lend = findnext(isequal(UInt8('\n')), code, b + 1)
63+
lend = isnothing(lend) ? lastindex(code) + 1 : lend
64+
s = String(code[b+1:lend-1]) # current line, containing no newlines
65+
line_b = 1
66+
for i in 1:pos.character
67+
line_b = nextind(s, line_b)
68+
end
69+
return b + line_b
70+
end
71+
72+
"""
73+
Convert a 1-based byte offset to a 0-based line and character number
74+
"""
75+
function offset_to_xy(fi::FileInfo, b::Integer)
76+
sf = JuliaSyntax.SourceFile(fi.parsed_stream)
77+
l, c = JuliaSyntax.source_location(sf, b)
78+
return Position(;line = l-1, character = c-1)
79+
end

0 commit comments

Comments
 (0)