|
| 1 | +------------------------------------------------------------------------------ |
| 2 | +-- Language Server Protocol -- |
| 3 | +-- -- |
| 4 | +-- Copyright (C) 2018-2024, AdaCore -- |
| 5 | +-- -- |
| 6 | +-- This is free software; you can redistribute it and/or modify it under -- |
| 7 | +-- terms of the GNU General Public License as published by the Free Soft- -- |
| 8 | +-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 9 | +-- sion. This software is distributed in the hope that it will be useful, -- |
| 10 | +-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- -- |
| 11 | +-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public -- |
| 12 | +-- License for more details. You should have received a copy of the GNU -- |
| 13 | +-- General Public License distributed with this software; see file -- |
| 14 | +-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy -- |
| 15 | +-- of the license. -- |
| 16 | +------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +with GNATCOLL.VFS; |
| 19 | + |
| 20 | +with LSP.Ada_Documents; |
| 21 | +with LSP.Client_Message_Receivers; |
| 22 | +with LSP.Structures; |
| 23 | +with LSP.Server_Notifications.DidChange; |
| 24 | + |
| 25 | +package body LSP.Ada_Did_Change_Document is |
| 26 | + |
| 27 | + type Did_Change_Job |
| 28 | + (Parent : not null access constant Ada_Did_Change_Handler) |
| 29 | + is limited new LSP.Server_Jobs.Server_Job with record |
| 30 | + Document : LSP.Ada_Documents.Document_Access; |
| 31 | + Message : LSP.Server_Messages.Server_Message_Access; |
| 32 | + Is_Done : Boolean := False; |
| 33 | + end record; |
| 34 | + |
| 35 | + type Did_Change_Job_Access is access all Did_Change_Job; |
| 36 | + |
| 37 | + overriding function Priority |
| 38 | + (Self : Did_Change_Job) return LSP.Server_Jobs.Job_Priority is |
| 39 | + (LSP.Server_Jobs.Fence); |
| 40 | + |
| 41 | + overriding function Is_Done (Self : Did_Change_Job) return Boolean is |
| 42 | + (Self.Is_Done); |
| 43 | + |
| 44 | + overriding procedure Execute |
| 45 | + (Self : in out Did_Change_Job; |
| 46 | + Client : |
| 47 | + in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class); |
| 48 | + |
| 49 | + overriding procedure Complete |
| 50 | + (Self : in out Did_Change_Job; |
| 51 | + Next : LSP.Server_Messages.Server_Message_Access); |
| 52 | + |
| 53 | + overriding function Message (Self : Did_Change_Job) |
| 54 | + return LSP.Server_Messages.Server_Message_Access is (Self.Message); |
| 55 | + |
| 56 | + function Is_Incremental |
| 57 | + (Changes : LSP.Structures.TextDocumentContentChangeEvent_Vector) |
| 58 | + return Boolean is |
| 59 | + (not Changes.Is_Empty and then Changes.First_Element.a_range.Is_Set); |
| 60 | + -- Changes has incrimental form (not just full text update) |
| 61 | + |
| 62 | + -------------- |
| 63 | + -- Complete -- |
| 64 | + -------------- |
| 65 | + |
| 66 | + overriding procedure Complete |
| 67 | + (Self : in out Did_Change_Job; |
| 68 | + Next : LSP.Server_Messages.Server_Message_Access) |
| 69 | + is |
| 70 | + use type LSP.Server_Messages.Server_Message_Access; |
| 71 | + use type GNATCOLL.VFS.Virtual_File; |
| 72 | + |
| 73 | + Message : LSP.Server_Notifications.DidChange.Notification renames |
| 74 | + LSP.Server_Notifications.DidChange.Notification (Self.Message.all); |
| 75 | + |
| 76 | + Changes : LSP.Structures.TextDocumentContentChangeEvent_Vector renames |
| 77 | + Message.Params.contentChanges; |
| 78 | + |
| 79 | + File : constant GNATCOLL.VFS.Virtual_File := |
| 80 | + Self.Parent.Context.To_File (Message.Params.textDocument.uri); |
| 81 | + |
| 82 | + begin |
| 83 | + if Next /= null and then |
| 84 | + Next.all in LSP.Server_Notifications.DidChange.Notification'Class |
| 85 | + then |
| 86 | + declare |
| 87 | + Change : LSP.Server_Notifications.DidChange.Notification renames |
| 88 | + LSP.Server_Notifications.DidChange.Notification (Next.all); |
| 89 | + |
| 90 | + Next_File : constant GNATCOLL.VFS.Virtual_File := |
| 91 | + Self.Parent.Context.To_File (Change.Params.textDocument.uri); |
| 92 | + begin |
| 93 | + if File = Next_File then |
| 94 | + -- However, we should skip the Indexing part (and |
| 95 | + -- non-incremental changes) if the next didChange message |
| 96 | + -- will re-change the text document. |
| 97 | + return; |
| 98 | + end if; |
| 99 | + end; |
| 100 | + end if; |
| 101 | + |
| 102 | + if not Is_Incremental (Changes) then |
| 103 | + Self.Document.Apply_Changes |
| 104 | + (Message.Params.textDocument.version, Changes); |
| 105 | + end if; |
| 106 | + |
| 107 | + for Context of Self.Parent.Context.Contexts_For_File (File) loop |
| 108 | + Context.Index_Document (Self.Document.all); |
| 109 | + end loop; |
| 110 | + |
| 111 | + -- Emit diagnostics |
| 112 | + Self.Parent.Context.Publish_Diagnostics (Self.Document); |
| 113 | + end Complete; |
| 114 | + |
| 115 | + ---------------- |
| 116 | + -- Create_Job -- |
| 117 | + ---------------- |
| 118 | + |
| 119 | + overriding function Create_Job |
| 120 | + (Self : Ada_Did_Change_Handler; |
| 121 | + Message : LSP.Server_Messages.Server_Message_Access) |
| 122 | + return LSP.Server_Jobs.Server_Job_Access |
| 123 | + is |
| 124 | + Value : LSP.Server_Notifications.DidChange.Notification renames |
| 125 | + LSP.Server_Notifications.DidChange.Notification (Message.all); |
| 126 | + |
| 127 | + URI : LSP.Structures.DocumentUri renames |
| 128 | + Value.Params.textDocument.uri; |
| 129 | + |
| 130 | + Document : constant LSP.Ada_Documents.Document_Access := |
| 131 | + Self.Context.Get_Open_Document (URI); |
| 132 | + |
| 133 | + Result : constant Did_Change_Job_Access := |
| 134 | + new Did_Change_Job' |
| 135 | + (Parent => Self'Unchecked_Access, |
| 136 | + Document => Document, |
| 137 | + Message => Message, |
| 138 | + Is_Done => False); |
| 139 | + begin |
| 140 | + return LSP.Server_Jobs.Server_Job_Access (Result); |
| 141 | + end Create_Job; |
| 142 | + |
| 143 | + ------------- |
| 144 | + -- Execute -- |
| 145 | + ------------- |
| 146 | + |
| 147 | + overriding procedure Execute |
| 148 | + (Self : in out Did_Change_Job; |
| 149 | + Client : |
| 150 | + in out LSP.Client_Message_Receivers.Client_Message_Receiver'Class) |
| 151 | + is |
| 152 | + use type LSP.Ada_Documents.Document_Access; |
| 153 | + |
| 154 | + Message : LSP.Server_Notifications.DidChange.Notification renames |
| 155 | + LSP.Server_Notifications.DidChange.Notification (Self.Message.all); |
| 156 | + |
| 157 | + Changes : LSP.Structures.TextDocumentContentChangeEvent_Vector renames |
| 158 | + Message.Params.contentChanges; |
| 159 | + begin |
| 160 | + Self.Is_Done := True; |
| 161 | + |
| 162 | + if Self.Document = null then |
| 163 | + return; |
| 164 | + end if; |
| 165 | + |
| 166 | + if Is_Incremental (Changes) then |
| 167 | + -- If we are applying incremental changes, we can't skip the |
| 168 | + -- call to Apply_Changes, since this would break synchronization. |
| 169 | + Self.Document.Apply_Changes |
| 170 | + (Message.Params.textDocument.version, Changes); |
| 171 | + |
| 172 | + -- However, we should skip the Indexing part if the next didChange |
| 173 | + -- message will re-change the text document. |
| 174 | + end if; |
| 175 | + |
| 176 | + -- Rest of the work in Complete routine |
| 177 | + end Execute; |
| 178 | + |
| 179 | +end LSP.Ada_Did_Change_Document; |
0 commit comments