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

Basic single file/dir with no mixfile support #562

Merged
merged 14 commits into from
Jun 30, 2021
10 changes: 6 additions & 4 deletions apps/language_server/lib/language_server/build.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ElixirLS.LanguageServer.Build do
alias ElixirLS.LanguageServer.{Server, JsonRpc, SourceFile, Diagnostics}

def build(parent, root_path, opts) do
def build(parent, root_path, opts) when is_binary(root_path) do
if Path.absname(File.cwd!()) != Path.absname(root_path) do
IO.puts("Skipping build because cwd changed from #{root_path} to #{File.cwd!()}")
{nil, nil}
Expand Down Expand Up @@ -37,6 +37,9 @@ defmodule ElixirLS.LanguageServer.Build do

{:error, mixfile_diagnostics} ->
Server.build_finished(parent, {:error, mixfile_diagnostics})

:no_mixfile ->
Server.build_finished(parent, {:no_mixfile, []})
end
end)

Expand Down Expand Up @@ -165,10 +168,9 @@ defmodule ElixirLS.LanguageServer.Build do
"No mixfile found in project. " <>
"To use a subdirectory, set `elixirLS.projectDir` in your settings"

JsonRpc.log_message(:error, msg <> ". Looked for mixfile at #{inspect(mixfile)}")
JsonRpc.show_message(:error, msg)
JsonRpc.log_message(:info, msg <> ". Looked for mixfile at #{inspect(mixfile)}")

{:error, [mixfile_diagnostic({Path.absname(mixfile), nil, msg}, :error)]}
:no_mixfile
end
end

Expand Down
26 changes: 17 additions & 9 deletions apps/language_server/lib/language_server/providers/formatting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
import ElixirLS.LanguageServer.Protocol, only: [range: 4]
alias ElixirLS.LanguageServer.SourceFile

def format(%SourceFile{} = source_file, uri, project_dir) do
def format(%SourceFile{} = source_file, uri, project_dir) when is_binary(project_dir) do
if can_format?(uri, project_dir) do
case SourceFile.formatter_opts(uri) do
{:ok, opts} ->
if should_format?(uri, project_dir, opts[:inputs]) do
formatted = IO.iodata_to_binary([Code.format_string!(source_file.text, opts), ?\n])

response =
source_file.text
|> String.myers_difference(formatted)
|> myers_diff_to_text_edits()

{:ok, response}
do_format(source_file, opts)
else
{:ok, []}
end
Expand All @@ -29,6 +22,21 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do

{:error, :internal_error, msg}
end
end

def format(%SourceFile{} = source_file, _uri, nil) do
do_format(source_file)
end

defp do_format(%SourceFile{text: text}, opts \\ []) do
formatted = IO.iodata_to_binary([Code.format_string!(text, opts), ?\n])

response =
text
|> String.myers_difference(formatted)
|> myers_diff_to_text_edits()

{:ok, response}
rescue
_e in [TokenMissingError, SyntaxError] ->
{:error, :internal_error, "Unable to format due to syntax error"}
Expand Down
Loading