Skip to content

Commit

Permalink
Added file watching (#164)
Browse files Browse the repository at this point in the history
* Added file watching

If the server supports it, we will now receive watched file
notifications for files that have been added / updated or deleted in
the project directories.

This will help determining whether we should recompile the whole
project if a dependency has changed, or if we should remove a module
from our compiled module cache.

* Started supervisor after init is sent
  • Loading branch information
scohen authored May 16, 2023
1 parent 63e5b5d commit 67238ef
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file's contents are auto-generated. Do not edit.
defmodule Lexical.Protocol.Types.DidChangeWatchedFiles.Registration.Options do
alias Lexical.Proto
alias Lexical.Protocol.Types
use Proto
deftype watchers: list_of(Types.FileSystemWatcher)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file's contents are auto-generated. Do not edit.
defmodule Lexical.Protocol.Types.FileSystemWatcher do
alias Lexical.Proto
alias Lexical.Protocol.Types
use Proto
deftype glob_pattern: Types.GlobPattern, kind: optional(Types.Watch.Kind)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file's contents are auto-generated. Do not edit.
defmodule Lexical.Protocol.Types.GlobPattern do
alias Lexical.Proto
alias Lexical.Protocol.Types
use Proto
defalias one_of([Types.Pattern, Types.RelativePattern])
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file's contents are auto-generated. Do not edit.
defmodule Lexical.Protocol.Types.RelativePattern do
alias Lexical.Proto
alias Lexical.Protocol.Types
use Proto
deftype base_uri: one_of([Types.Workspace.Folder, string()]), pattern: Types.Pattern
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file's contents are auto-generated. Do not edit.
defmodule Lexical.Protocol.Types.Watch.Kind do
alias Lexical.Proto
use Proto
defenum create: 1, change: 2, delete: 4
end
7 changes: 7 additions & 0 deletions apps/protocol/lib/lexical/protocol/json_rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Lexical.Protocol.JsonRpc do
alias Lexical.Protocol.Notifications
alias Lexical.Protocol.Requests

require Logger

@crlf "\r\n"

def decode(message_string) do
Expand Down Expand Up @@ -30,6 +32,11 @@ defmodule Lexical.Protocol.JsonRpc do
{:ok, json_rpc}
end

defp do_decode(%{"id" => id, "result" => nil}) do
Logger.info("Reply for id #{id} had no body")
:error
end

defp do_decode(%{"method" => method, "id" => _id} = request) do
Requests.decode(method, request)
end
Expand Down
48 changes: 36 additions & 12 deletions apps/server/lib/lexical/server/state.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
defmodule Lexical.Server.State do
alias Lexical.Document

alias Lexical.Protocol.Notifications.{
DidChange,
DidChangeConfiguration,
DidClose,
DidOpen,
DidSave,
Initialized
}

alias Lexical.Protocol.Id
alias Lexical.Protocol.Notifications.DidChange
alias Lexical.Protocol.Notifications.DidChangeConfiguration
alias Lexical.Protocol.Notifications.DidClose
alias Lexical.Protocol.Notifications.DidOpen
alias Lexical.Protocol.Notifications.DidSave
alias Lexical.Protocol.Notifications.Exit
alias Lexical.Protocol.Notifications.Initialized
alias Lexical.Protocol.Requests.Initialize
alias Lexical.Protocol.Requests.RegisterCapability
alias Lexical.Protocol.Requests.Shutdown
alias Lexical.Protocol.Responses
alias Lexical.Protocol.Types
alias Lexical.Protocol.Types.CodeAction
alias Lexical.Protocol.Types.Completion
alias Lexical.Protocol.Types.DidChangeWatchedFiles
alias Lexical.Protocol.Types.FileSystemWatcher
alias Lexical.Protocol.Types.Registration
alias Lexical.Protocol.Types.TextDocument
alias Lexical.RemoteControl.Api
alias Lexical.Server.CodeIntelligence
alias Lexical.Server.Configuration
alias Lexical.Server.Project
alias Lexical.Server.Transport

require CodeAction.Kind
Expand All @@ -43,12 +45,13 @@ defmodule Lexical.Server.State do
new_state = %__MODULE__{state | configuration: config, initialized?: true}
Logger.info("Starting project at uri #{config.project.root_uri}")

Lexical.Server.Project.Supervisor.start(config.project)

event.id
|> initialize_result()
|> Transport.write()

Transport.write(registrations())

Project.Supervisor.start(config.project)
{:ok, new_state}
end

Expand Down Expand Up @@ -168,6 +171,27 @@ defmodule Lexical.Server.State do
{:ok, state}
end

defp registrations do
RegisterCapability.new(id: Id.next(), registrations: [file_watcher_registration()])
end

@did_changed_watched_files_id "-42"
@watched_extensions ~w(ex exs)
defp file_watcher_registration do
extension_glob = "{" <> Enum.join(@watched_extensions, ",") <> "}"

watchers = [
FileSystemWatcher.new(glob_pattern: "**/mix.lock"),
FileSystemWatcher.new(glob_pattern: "**/*.#{extension_glob}")
]

Registration.new(
id: @did_changed_watched_files_id,
method: "workspace/didChangeWatchedFiles",
register_options: DidChangeWatchedFiles.Registration.Options.new(watchers: watchers)
)
end

def initialize_result(event_id) do
sync_options =
TextDocument.Sync.Options.new(open_close: true, change: :incremental, save: true)
Expand Down

0 comments on commit 67238ef

Please sign in to comment.