|
| 1 | +defmodule Expert.Provider.Handlers.CodeActionTest do |
| 2 | + alias Expert.EngineApi |
| 3 | + alias Expert.Protocol.Convert |
| 4 | + alias Expert.Provider.Handlers |
| 5 | + alias Forge.Document |
| 6 | + alias GenLSP.Requests.TextDocumentCodeAction |
| 7 | + alias GenLSP.Structures |
| 8 | + |
| 9 | + import Forge.EngineApi.Messages |
| 10 | + import Forge.Test.Fixtures |
| 11 | + |
| 12 | + use ExUnit.Case, async: false |
| 13 | + |
| 14 | + setup_all do |
| 15 | + start_supervised!({Document.Store, derive: [analysis: &Forge.Ast.analyze/1]}) |
| 16 | + project = project(:navigations) |
| 17 | + |
| 18 | + start_supervised!({DynamicSupervisor, Expert.Project.DynamicSupervisor.options()}) |
| 19 | + start_supervised!({Expert.Project.Supervisor, project}) |
| 20 | + |
| 21 | + EngineApi.register_listener(project, self(), [project_compiled()]) |
| 22 | + EngineApi.schedule_compile(project, true) |
| 23 | + |
| 24 | + assert_receive project_compiled(), 5000 |
| 25 | + |
| 26 | + {:ok, project: project} |
| 27 | + end |
| 28 | + |
| 29 | + def build_request(path, {start_line, start_char}, {end_line, end_char}) do |
| 30 | + uri = Document.Path.ensure_uri(path) |
| 31 | + |
| 32 | + with {:ok, _} <- Document.Store.open_temporary(uri) do |
| 33 | + req = %TextDocumentCodeAction{ |
| 34 | + id: Expert.Protocol.Id.next(), |
| 35 | + params: %Structures.CodeActionParams{ |
| 36 | + text_document: %Structures.TextDocumentIdentifier{uri: uri}, |
| 37 | + context: %Structures.CodeActionContext{ |
| 38 | + trigger_kind: 1, |
| 39 | + only: nil, |
| 40 | + diagnostics: [ |
| 41 | + %Structures.Diagnostic{ |
| 42 | + range: %Structures.Range{ |
| 43 | + start: %Structures.Position{line: start_line, character: start_char}, |
| 44 | + end: %Structures.Position{line: end_line, character: end_char} |
| 45 | + }, |
| 46 | + message: "Test diagnostic", |
| 47 | + severity: 1, |
| 48 | + source: "TestSource" |
| 49 | + } |
| 50 | + ] |
| 51 | + }, |
| 52 | + range: %Structures.Range{ |
| 53 | + start: %Structures.Position{line: start_line, character: start_char}, |
| 54 | + end: %Structures.Position{line: end_line, character: end_char} |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Convert.to_native(req) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def handle(request, project) do |
| 64 | + config = Expert.Configuration.new(project: project) |
| 65 | + Handlers.CodeAction.handle(request, config) |
| 66 | + end |
| 67 | + |
| 68 | + describe "handle code actions" do |
| 69 | + test "returns code actions for a given range", %{project: project} do |
| 70 | + uses_file_path = file_path(project, Path.join("lib", "uses.ex")) |
| 71 | + {:ok, request} = build_request(uses_file_path, {4, 4}, {4, 31}) |
| 72 | + |
| 73 | + assert {:ok, _actions} = handle(request, project) |
| 74 | + end |
| 75 | + end |
| 76 | +end |
0 commit comments