Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AUTO] Format files using DocumentFormat #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
58 changes: 29 additions & 29 deletions src/JuliaWorkspaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -261,29 +261,29 @@ 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
end

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
Expand Down
132 changes: 66 additions & 66 deletions src/URIs2/URIs2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
# <user>@<auth>
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
# <user>@<auth>
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
# <auth>:<port>
else
# <auth>:<port>
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
Expand Down
Loading