Fix exile process read new line out of order spellcheck

This commit is contained in:
Simon Prévost 2024-04-04 09:34:42 -04:00
parent dacb89ba4d
commit ae92a1cb87
2 changed files with 18 additions and 5 deletions

View File

@ -47,10 +47,11 @@ COPY config config
RUN mix deps.get --only prod
RUN mix deps.compile --only prod
COPY vendor vendor
COPY vendor/language_tool/priv/ vendor/language_tool/priv/
RUN cd ./vendor/language_tool/priv/native/languagetool && ./gradlew shadowJar
RUN cp ./vendor/language_tool/priv/native/languagetool/app/build/libs/language-tool.jar priv/native/language-tool.jar
COPY vendor vendor
COPY lib lib
RUN mix compile --only prod

View File

@ -82,17 +82,29 @@ defmodule LanguageTool.Backend do
end
defp process_check(process, lang, text) do
lang = sanitize_lang(lang)
Exile.Process.write(process, IO.iodata_to_binary([String.pad_trailing(lang, 7), text, "\n"]))
write_process(process, lang, text)
with {:ok, data} <- Exile.Process.read(process),
with {:ok, data} <- read_process_until_result(process),
{:ok, data} <- Jason.decode(data) do
data
else
_ -> nil
_ ->
nil
end
end
defp read_process_until_result(process) do
case Exile.Process.read(process) do
{:ok, "\n"} -> read_process_until_result(process)
result -> result
end
end
defp write_process(process, lang, text) do
lang = sanitize_lang(lang)
Exile.Process.write(process, IO.iodata_to_binary([String.pad_trailing(lang, 7), text, "\n"]))
end
defp sanitize_lang(lang) do
if lang === "en" do
"en-US"