|
1 | 1 | import JuliaLowering: SyntaxTree, JuliaLowering as JL |
2 | 2 | import JuliaSyntax: @K_str, @KSet_str, JuliaSyntax as JS |
3 | 3 |
|
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 |
11 | 4 | # get_fileinfo(s::ServerState, t::TextDocumentPositionParams) = get_fileinfo(s, t.textDocument) |
12 | 5 | get_fileinfo(s::ServerState, t::CompletionParams) = get_fileinfo(s, t.textDocument) |
13 | 6 |
|
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 | | - |
45 | 7 | """ |
46 | 8 | Like `Base.unique`, but over node ids, and with this comment promising that the |
47 | 9 | lowest-index copy of each node is kept. |
|
0 commit comments