From 0c8e3cd8223e26db22ec7e419c7e74a9bb91a59b Mon Sep 17 00:00:00 2001 From: davidanthoff Date: Thu, 19 Jan 2023 01:29:45 +0000 Subject: [PATCH] Format files using DocumentFormat --- src/JuliaWorkspaces.jl | 58 +++++++------- src/URIs2/URIs2.jl | 132 ++++++++++++++++---------------- src/URIs2/uri_helpers.jl | 12 +-- src/URIs2/vendored_from_uris.jl | 4 +- src/compat.jl | 6 +- src/textdocument.jl | 18 ++--- test/test_uris2.jl | 6 +- 7 files changed, 118 insertions(+), 118 deletions(-) diff --git a/src/JuliaWorkspaces.jl b/src/JuliaWorkspaces.jl index 2c46718..e90ac9a 100644 --- a/src/JuliaWorkspaces.jl +++ b/src/JuliaWorkspaces.jl @@ -56,12 +56,12 @@ function JuliaWorkspace(workspace_folders::Set{URI}) text_documents = isempty(workspace_folders) ? Dict{URI,TextDocument}() : merge((read_path_into_textdocuments(path) for path in workspace_folders)...) toml_syntax_trees = Dict{URI,Dict}() - for (k,v) in pairs(text_documents) + for (k, v) in pairs(text_documents) if endswith(lowercase(string(k)), ".toml") # try - toml_syntax_trees[k] = parse_toml_file(get_text(v)) + toml_syntax_trees[k] = parse_toml_file(get_text(v)) # catch err - # TODO Add some diagnostics + # TODO Add some diagnostics # end end end @@ -78,13 +78,13 @@ end function is_path_project_file(path) basename_lower_case = basename(lowercase(path)) - return basename_lower_case=="project.toml" || basename_lower_case=="juliaproject.toml" + return basename_lower_case == "project.toml" || basename_lower_case == "juliaproject.toml" end function is_path_manifest_file(path) basename_lower_case = basename(lowercase(path)) - return basename_lower_case=="manifest.toml" || basename_lower_case=="juliamanifest.toml" + return basename_lower_case == "manifest.toml" || basename_lower_case == "juliamanifest.toml" end function read_textdocument_from_uri(uri::URI) @@ -109,21 +109,21 @@ function read_path_into_textdocuments(uri::URI) if true #T TODO Move this check into the LS logic - # if load_rootpath(path) - # TODO Think about this try catch block + # if load_rootpath(path) + # TODO Think about this try catch block # try - for (root, _, files) in walkdir(path, onerror=x -> x) - for file in files - - filepath = joinpath(root, file) - if is_path_project_file(filepath) || is_path_manifest_file(filepath) - uri = filepath2uri(filepath) - doc = read_textdocument_from_uri(uri) - doc === nothing && continue - result[uri] = doc - end + for (root, _, files) in walkdir(path, onerror=x -> x) + for file in files + + filepath = joinpath(root, file) + if is_path_project_file(filepath) || is_path_manifest_file(filepath) + uri = filepath2uri(filepath) + doc = read_textdocument_from_uri(uri) + doc === nothing && continue + result[uri] = doc end end + end # catch err # is_walkdir_error(err) || rethrow() # end @@ -137,7 +137,7 @@ function add_workspace_folder(jw::JuliaWorkspace, folder::URI) new_toml_syntax_trees = copy(jw._toml_syntax_trees) additional_documents = read_path_into_textdocuments(folder) - for (k,v) in pairs(additional_documents) + for (k, v) in pairs(additional_documents) if endswith(lowercase(string(k)), ".toml") try new_toml_syntax_trees[k] = parse_toml_file(get_text(v)) @@ -158,7 +158,7 @@ function remove_workspace_folder(jw::JuliaWorkspace, folder::URI) new_text_documents = filter(jw._text_documents) do i # TODO Eventually use FilePathsBase functionality to properly test this - return any(startswith(string(i.first), string(j)) for j in new_roots ) + return any(startswith(string(i.first), string(j)) for j in new_roots) end new_toml_syntax_trees = filter(jw._toml_syntax_trees) do i @@ -174,7 +174,7 @@ function add_file(jw::JuliaWorkspace, uri::URI) new_jw = jw - if new_doc!==nothing + if new_doc !== nothing new_text_documents = copy(jw._text_documents) new_text_documents[uri] = new_doc @@ -189,7 +189,7 @@ function add_file(jw::JuliaWorkspace, uri::URI) nothing end - new_jw = JuliaWorkspace(jw._workspace_folders, new_text_documents, new_toml_syntax_trees, semantic_pass_toml_files(new_toml_syntax_trees)...) + new_jw = JuliaWorkspace(jw._workspace_folders, new_text_documents, new_toml_syntax_trees, semantic_pass_toml_files(new_toml_syntax_trees)...) end return new_jw @@ -200,7 +200,7 @@ function update_file(jw::JuliaWorkspace, uri::URI) new_jw = jw - if new_doc!==nothing + if new_doc !== nothing new_text_documents = copy(jw._text_documents) new_text_documents[uri] = new_doc @@ -240,11 +240,11 @@ function semantic_pass_toml_files(toml_syntax_trees) # Extract all packages & paths with a manifest packages = Dict{URI,JuliaPackage}() paths_with_manifest = Dict{String,Dict}() - for (k,v) in pairs(toml_syntax_trees) + for (k, v) in pairs(toml_syntax_trees) # TODO Maybe also check the filename here and only do the package detection for Project.toml and JuliaProject.toml if haskey(v, "name") && haskey(v, "uuid") && haskey(v, "version") parsed_uuid = tryparse(UUID, v["uuid"]) - if parsed_uuid!==nothing + if parsed_uuid !== nothing folder_uri = k |> uri2filepath |> dirname |> filepath2uri packages[folder_uri] = JuliaPackage(k, v["name"], parsed_uuid) end @@ -261,21 +261,21 @@ function semantic_pass_toml_files(toml_syntax_trees) # Extract all projects projects = Dict{URI,JuliaProject}() - for (k,_) in pairs(toml_syntax_trees) + for (k, _) in pairs(toml_syntax_trees) path = uri2filepath(k) dname = dirname(path) filename = basename(path) filename_lc = lowercase(filename) - if (filename_lc=="project.toml" || filename_lc=="juliaproject.toml" ) && haskey(paths_with_manifest, dname) + if (filename_lc == "project.toml" || filename_lc == "juliaproject.toml") && haskey(paths_with_manifest, dname) manifest_content = paths_with_manifest[dname] manifest_content isa Dict || continue deved_packages = Dict{URI,JuliaDevedPackage}() manifest_version = get(manifest_content, "manifest_format", "1.0") - manifest_deps = if manifest_version=="1.0" + manifest_deps = if manifest_version == "1.0" manifest_content - elseif manifest_version=="2.0" && haskey(manifest_content, "deps") && manifest_content["deps"] isa Dict + elseif manifest_version == "2.0" && haskey(manifest_content, "deps") && manifest_content["deps"] isa Dict manifest_content["deps"] else continue @@ -283,7 +283,7 @@ function semantic_pass_toml_files(toml_syntax_trees) for (k_entry, v_entry) in pairs(manifest_deps) v_entry isa Vector || continue - length(v_entry)==1 || continue + length(v_entry) == 1 || continue v_entry[1] isa Dict || continue haskey(v_entry[1], "path") || continue haskey(v_entry[1], "uuid") || continue diff --git a/src/URIs2/URIs2.jl b/src/URIs2/URIs2.jl index 39d7f28..ede37f0 100644 --- a/src/URIs2/URIs2.jl +++ b/src/URIs2/URIs2.jl @@ -14,26 +14,26 @@ end @static if Sys.iswindows() function Base.:(==)(a::URI, b::URI) - if a.scheme=="file" && b.scheme=="file" + if a.scheme == "file" && b.scheme == "file" a_path_norm = lowercase(a.path) b_path_norm = lowercase(b.path) return a.scheme == b.scheme && - a.authority == b.authority && - a_path_norm == b_path_norm && - a.query == b.query && - a.fragment == b.fragment + a.authority == b.authority && + a_path_norm == b_path_norm && + a.query == b.query && + a.fragment == b.fragment else return a.scheme == b.scheme && - a.authority == b.authority && - a.path == b.path && - a.query == b.query && - a.fragment == b.fragment + a.authority == b.authority && + a.path == b.path && + a.query == b.query && + a.fragment == b.fragment end end function Base.hash(a::URI, h::UInt) - if a.scheme=="file" + if a.scheme == "file" path_norm = lowercase(a.path) return hash((a.scheme, a.authority, path_norm, a.query, a.fragment), h) else @@ -49,14 +49,14 @@ end function URI(value::AbstractString) m = match(r"^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?", value) - m===nothing && error("Invalid argument.") + m === nothing && error("Invalid argument.") return URI( m.captures[2], - m.captures[4]===nothing ? nothing : percent_decode(m.captures[4]), - m.captures[5]===nothing ? nothing : percent_decode(m.captures[5]), - m.captures[7]===nothing ? nothing : percent_decode(m.captures[7]), - m.captures[9]===nothing ? nothing : percent_decode(m.captures[9]) + m.captures[4] === nothing ? nothing : percent_decode(m.captures[4]), + m.captures[5] === nothing ? nothing : percent_decode(m.captures[5]), + m.captures[7] === nothing ? nothing : percent_decode(m.captures[7]), + m.captures[9] === nothing ? nothing : percent_decode(m.captures[9]) ) end @@ -68,58 +68,58 @@ function URI(; path::AbstractString="", query::Union{AbstractString,Nothing}=nothing, fragment::Union{AbstractString,Nothing}=nothing - ) +) return URI(scheme, authority, path, query, fragment) end @inline function is_rfc3986_unreserved(c::Char) return 'A' <= c <= 'Z' || - 'a' <= c <= 'z' || - '0' <= c <= '9' || - c == '-' || - c == '.' || - c == '_' || - c == '~' + 'a' <= c <= 'z' || + '0' <= c <= '9' || + c == '-' || + c == '.' || + c == '_' || + c == '~' end @inline function is_rfc3986_sub_delim(c::Char) return c == '!' || - c == '$' || - c == '&' || - c == '\'' || - c == '(' || - c == ')' || - c == '*' || - c == '+' || - c == ',' || - c == ';' || - c == '=' + c == '$' || + c == '&' || + c == '\'' || + c == '(' || + c == ')' || + c == '*' || + c == '+' || + c == ',' || + c == ';' || + c == '=' end @inline function is_rfc3986_pchar(c::Char) return is_rfc3986_unreserved(c) || - is_rfc3986_sub_delim(c) || - c == ':' || - c == '@' + is_rfc3986_sub_delim(c) || + c == ':' || + c == '@' end @inline function is_rfc3986_query(c::Char) - return is_rfc3986_pchar(c) || c=='/' || c=='?' + return is_rfc3986_pchar(c) || c == '/' || c == '?' end @inline function is_rfc3986_fragment(c::Char) - return is_rfc3986_pchar(c) || c=='/' || c=='?' + return is_rfc3986_pchar(c) || c == '/' || c == '?' end @inline function is_rfc3986_userinfo(c::Char) return is_rfc3986_unreserved(c) || - is_rfc3986_sub_delim(c) || - c == ':' + is_rfc3986_sub_delim(c) || + c == ':' end @inline function is_rfc3986_reg_name(c::Char) return is_rfc3986_unreserved(c) || - is_rfc3986_sub_delim(c) + is_rfc3986_sub_delim(c) end function encode(io::IO, s::AbstractString, issafe::Function) @@ -134,14 +134,14 @@ function encode(io::IO, s::AbstractString, issafe::Function) end @inline function is_ipv4address(s::AbstractString) - if length(s)==1 + if length(s) == 1 return '0' <= s[1] <= '9' - elseif length(s)==2 + elseif length(s) == 2 return '1' <= s[1] <= '9' && '0' <= s[2] <= '9' - elseif length(s)==3 - return (s[1]=='1' && '0' <= s[2] <= '9' && '0' <= s[3] <= '9') || - (s[1]=='2' && '0' <= s[2] <= '4' && '0' <= s[3] <= '9') || - (s[1]=='2' && s[2] == '5' && '0' <= s[3] <= '5') + elseif length(s) == 3 + return (s[1] == '1' && '0' <= s[2] <= '9' && '0' <= s[3] <= '9') || + (s[1] == '2' && '0' <= s[2] <= '4' && '0' <= s[3] <= '9') || + (s[1] == '2' && s[2] == '5' && '0' <= s[3] <= '5') else return false end @@ -173,44 +173,44 @@ function Base.print(io::IO, uri::URI) query = uri.query fragment = uri.fragment - if scheme!==nothing + if scheme !== nothing print(io, scheme) print(io, ':') - end + end - if authority!==nothing + if authority !== nothing print(io, "//") - idx = findfirst("@", authority) - if idx !== nothing - # @ - userinfo = SubString(authority, 1:idx.start-1) - host_and_port = SubString(authority, idx.start + 1) - encode(io, userinfo, is_rfc3986_userinfo) + idx = findfirst("@", authority) + if idx !== nothing + # @ + userinfo = SubString(authority, 1:idx.start-1) + host_and_port = SubString(authority, idx.start + 1) + encode(io, userinfo, is_rfc3986_userinfo) print(io, '@') else host_and_port = SubString(authority, 1) - end + end - idx3 = findfirst(":", host_and_port) - if idx3 === nothing + idx3 = findfirst(":", host_and_port) + if idx3 === nothing encode_host(io, host_and_port) - else - # : + else + # : encode_host(io, SubString(host_and_port, 1:idx3.start-1)) - print(io, SubString(host_and_port, idx3.start)) + print(io, SubString(host_and_port, idx3.start)) end - end + end - # Append path - encode_path(io, path) + # Append path + encode_path(io, path) - if query!==nothing + if query !== nothing print(io, '?') encode(io, query, is_rfc3986_query) end - if fragment!==nothing + if fragment !== nothing print(io, '#') encode(io, fragment, is_rfc3986_fragment) end diff --git a/src/URIs2/uri_helpers.jl b/src/URIs2/uri_helpers.jl index b85f392..a0b5581 100644 --- a/src/URIs2/uri_helpers.jl +++ b/src/URIs2/uri_helpers.jl @@ -7,13 +7,13 @@ function uri2filepath(uri::URI) path = uri.path host = uri.authority - if host!==nothing && host != "" && length(path) > 1 + if host !== nothing && host != "" && length(path) > 1 # unc path: file://shares/c$/far/boo value = "//$host$path" elseif length(path) >= 3 && - path[1] == '/' && - isascii(path[2]) && isletter(path[2]) && - path[3] == ':' + path[1] == '/' && + isascii(path[2]) && isletter(path[2]) && + path[3] == ':' # windows drive letter: file:///c:/far/boo value = lowercase(path[2]) * path[3:end] else @@ -42,14 +42,14 @@ function filepath2uri(path::String) if startswith(path, "//") # UNC path //foo/bar/foobar idx = findnext("/", path, 3) - if idx===nothing + if idx === nothing authority = path[3:end] path = "/" else authority = path[3:idx.start-1] path = path[idx.start:end] end - elseif length(path)>=2 && isascii(path[1]) && isletter(path[1]) && path[2]==':' + elseif length(path) >= 2 && isascii(path[1]) && isletter(path[1]) && path[2] == ':' path = string('/', lowercase(path[1]), SubString(path, 2)) end diff --git a/src/URIs2/vendored_from_uris.jl b/src/URIs2/vendored_from_uris.jl index e659e2c..1342e8f 100644 --- a/src/URIs2/vendored_from_uris.jl +++ b/src/URIs2/vendored_from_uris.jl @@ -11,7 +11,7 @@ Get a `Vector{UInt8}`, a vector of bytes of a string. function _bytes end _bytes(s::SubArray{UInt8}) = unsafe_wrap(Array, pointer(s), length(s)) -_bytes(s::Union{Vector{UInt8}, Base.CodeUnits}) = _bytes(String(s)) +_bytes(s::Union{Vector{UInt8},Base.CodeUnits}) = _bytes(String(s)) _bytes(s::AbstractString) = codeunits(s) _bytes(s::Vector{UInt8}) = s @@ -43,7 +43,7 @@ encoded within the query part of a URI. """ escapeuri(key, value) = string(escapeuri(key), "=", escapeuri(value)) escapeuri(key, values::Vector) = escapeuri(key => v for v in values) -escapeuri(query) = isempty(query) ? absent : join((escapeuri(k, v) for (k,v) in query), "&") +escapeuri(query) = isempty(query) ? absent : join((escapeuri(k, v) for (k, v) in query), "&") escapeuri(nt::NamedTuple) = escapeuri(pairs(nt)) """ diff --git a/src/compat.jl b/src/compat.jl index 4edc10a..4f03f14 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -8,9 +8,9 @@ _Z = UInt32('Z') _z = UInt32('z') a::UInt32 = base <= 36 ? 10 : 36 - d = _0 <= _c <= _9 ? _c-_0 : - _A <= _c <= _Z ? _c-_A+ UInt32(10) : - _a <= _c <= _z ? _c-_a+a : UInt32(base) + d = _0 <= _c <= _9 ? _c - _0 : + _A <= _c <= _Z ? _c - _A + UInt32(10) : + _a <= _c <= _z ? _c - _a + a : UInt32(base) end @inline function uuid_kernel(s, i, u) diff --git a/src/textdocument.jl b/src/textdocument.jl index a8349a4..e707c9a 100644 --- a/src/textdocument.jl +++ b/src/textdocument.jl @@ -6,7 +6,7 @@ struct TextDocument _line_indices::Union{Nothing,Vector{Int}} _language_id::String - function TextDocument(uri::URI, text::AbstractString, version::Int, lid = nothing) + function TextDocument(uri::URI, text::AbstractString, version::Int, lid=nothing) # TODO Remove this check eventually occursin('\0', text) && throw(LSInvalidFile("Tried to set a text with an embedded NULL as the document content.")) @@ -61,9 +61,9 @@ function index_at(doc::TextDocument, line::Int, character::Int, forgiving_mode=f throw(LSOffsetError("index_at crashed. More diagnostics:\nline=$line\nline_indices='$line_indices'")) end - line_index = line_indices[line + 1] + line_index = line_indices[line+1] - next_line_index = line + 1 < length(line_indices) ? line_indices[line + 2] : nextind(text, lastindex(text)) + next_line_index = line + 1 < length(line_indices) ? line_indices[line+2] : nextind(text, lastindex(text)) pos = line_index @@ -115,7 +115,7 @@ function _compute_line_indices(text) while ind <= lastindex(text) c = text[ind] if c == '\n' || c == '\r' - if c == '\r' && ind + 1 <= lastindex(text) && text[ind + 1] == '\n' + if c == '\r' && ind + 1 <= lastindex(text) && text[ind+1] == '\n' ind += 1 end push!(line_indices, ind + 1) @@ -158,12 +158,12 @@ function _get_line_of(doc::TextDocument, offset::Integer) else line = 1 while line < nlines - if line_offsets[line] <= offset < line_offsets[line + 1] + if line_offsets[line] <= offset < line_offsets[line+1] break end line += 1 end -end + end return line, line_offsets[line] end @@ -178,7 +178,7 @@ function _compute_line_offsets(text) while ind <= lastindex(text) c = text[ind] nl = c == '\n' || c == '\r' - if c == '\r' && ind + 1 <= lastindex(text) && text[ind + 1] == '\n' + if c == '\r' && ind + 1 <= lastindex(text) && text[ind+1] == '\n' ind += 1 end nl && push!(line_offsets, ind) @@ -209,7 +209,7 @@ function get_offset(doc::TextDocument, line::Integer, character::Integer) line_offsets = get_line_offsets(doc) io = IOBuffer(get_text(doc)) try - seek(io, line_offsets[line + 1]) + seek(io, line_offsets[line+1]) while character > 0 c = read(io, Char) character -= 1 @@ -248,7 +248,7 @@ function get_position_from_offset(doc::TextDocument, offset::Integer) c = read(io, Char) character += 1 if UInt32(c) >= 0x010000 - character += 1 + character += 1 end end close(io) diff --git a/test/test_uris2.jl b/test/test_uris2.jl index dfdc8a8..4097464 100644 --- a/test/test_uris2.jl +++ b/test/test_uris2.jl @@ -16,7 +16,7 @@ end if Sys.iswindows() @test filepath2uri("c:\\win\\path") |> string == "file:///c%3A/win/path" @test filepath2uri("c:\\win/path") |> string == "file:///c%3A/win/path" - # else TODO Put this else back in once we support these paths on Unix + # else TODO Put this else back in once we support these paths on Unix # @test filepath2uri("c:\\win\\path") |> string == "file:///c%3A%5Cwin%5Cpath" # @test filepath2uri("c:\\win/path") |> string == "file:///c%3A%5Cwin/path" end @@ -59,7 +59,7 @@ end @testitem "parse" begin using JuliaWorkspaces.URIs2 - + value = URI("http:/api/files/test.me?t=1234") @test value.scheme == "http" @test_broken value.authority == "" @@ -79,7 +79,7 @@ end @test value.authority == "" @test value.path == "/c:/test/me" @test value.fragment === nothing - @test value.query=== nothing + @test value.query === nothing @test uri2filepath(value) == (Sys.iswindows() ? "c:\\test\\me" : "c:/test/me") value = URI("file://shares/files/c%23/p.cs")