[gnome-builder] lsp: Move text edit decoding from rename provider to utils



commit 316429f0132e64679397c1620f55446f52ff1fb9
Author: Tom A. Wagner <tom a wagner protonmail com>
Date:   Thu Aug 12 10:07:49 2021 +0200

    lsp: Move text edit decoding from rename provider to utils

 src/libide/lsp/ide-lsp-rename-provider.c | 51 ++--------------------------
 src/libide/lsp/ide-lsp-util.c            | 58 ++++++++++++++++++++++++++++++++
 src/libide/lsp/ide-lsp-util.h            |  7 ++--
 3 files changed, 66 insertions(+), 50 deletions(-)
---
diff --git a/src/libide/lsp/ide-lsp-rename-provider.c b/src/libide/lsp/ide-lsp-rename-provider.c
index bdae4de7b..6f9cc65b0 100644
--- a/src/libide/lsp/ide-lsp-rename-provider.c
+++ b/src/libide/lsp/ide-lsp-rename-provider.c
@@ -28,6 +28,7 @@
 
 #include "ide-lsp-client.h"
 #include "ide-lsp-rename-provider.h"
+#include "ide-lsp-util.h"
 
 typedef struct
 {
@@ -144,52 +145,6 @@ ide_lsp_rename_provider_init (IdeLspRenameProvider *self)
 {
 }
 
-/*
- * Parsing a TextEdit Variant happens here. See specification
- * https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textEdit
- * for further details
- */
-static IdeTextEdit *
-_extract_text_edit (GFile *gfile, GVariant *change)
-{
-  g_autoptr(IdeLocation) begin_location = NULL;
-  g_autoptr(IdeLocation) end_location = NULL;
-  g_autoptr(IdeRange) range = NULL;
-  const gchar *new_text = NULL;
-  gboolean success;
-  struct {
-    gint64 line;
-    gint64 column;
-  } begin, end;
-
-  success = JSONRPC_MESSAGE_PARSE (change,
-    "range", "{",
-      "start", "{",
-        "line", JSONRPC_MESSAGE_GET_INT64 (&begin.line),
-        "character", JSONRPC_MESSAGE_GET_INT64 (&begin.column),
-      "}",
-      "end", "{",
-        "line", JSONRPC_MESSAGE_GET_INT64 (&end.line),
-        "character", JSONRPC_MESSAGE_GET_INT64 (&end.column),
-      "}",
-    "}",
-    "newText", JSONRPC_MESSAGE_GET_STRING (&new_text)
-  );
-
-  if (!success)
-    {
-      IDE_TRACE_MSG ("Failed to extract change from variant");
-      return NULL;
-    }
-
-  begin_location = ide_location_new (gfile, begin.line, begin.column);
-  end_location = ide_location_new (gfile, end.line, end.column);
-  range = ide_range_new (begin_location, end_location);
-
-  return ide_text_edit_new (range, new_text);
-
-}
-
 static void
 ide_lsp_rename_provider_rename_cb_changes (IdeTask *task, GVariant *return_value)
 {
@@ -217,7 +172,7 @@ ide_lsp_rename_provider_rename_cb_changes (IdeTask *task, GVariant *return_value
 
       while (g_variant_iter_loop (&changes_iter, "v", &change))
         {
-          IdeTextEdit *edit = _extract_text_edit (gfile, change);
+          IdeTextEdit *edit = ide_lsp_decode_text_edit (change, gfile);
           if (edit != NULL)
             g_ptr_array_add (ret, edit);
         }
@@ -257,7 +212,7 @@ ide_lsp_rename_provider_rename_cb_document_changes (IdeTask *task, GVariant *ret
 
       while (g_variant_iter_loop (edits, "v", &edit))
         {
-          IdeTextEdit *tedit = _extract_text_edit (gfile, edit);
+          IdeTextEdit *tedit = ide_lsp_decode_text_edit (edit, gfile);
           if (tedit != NULL)
             g_ptr_array_add (ret, tedit);
         }
diff --git a/src/libide/lsp/ide-lsp-util.c b/src/libide/lsp/ide-lsp-util.c
index 2baf4ce4e..9ec384593 100644
--- a/src/libide/lsp/ide-lsp-util.c
+++ b/src/libide/lsp/ide-lsp-util.c
@@ -18,6 +18,8 @@
  * SPDX-License-Identifier: GPL-3.0-or-later
  */
 
+#include <jsonrpc-glib.h>
+
 #include "ide-lsp-util.h"
 
 IdeSymbolKind
@@ -86,3 +88,59 @@ ide_lsp_decode_completion_kind (guint kind)
 
   return kind;
 }
+
+/**
+ * ide_lsp_decode_text_edit:
+ * @text_edit: an lsp text edit encoded in a variant
+ * @file: The file the edit should be applied to
+ *
+ * Attempt to parse an encoded LSP text edit into an #IdeTextEdit.
+ *
+ * See specification
+ * https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textEdit
+ * for further details.
+ *
+ * Returns: (transfer full) (nullable): an #IdeTextEdit
+ *
+ * Since: 41
+ */
+IdeTextEdit *
+ide_lsp_decode_text_edit (GVariant *text_edit, GFile *gfile)
+{
+  g_autoptr(IdeLocation) begin_location = NULL;
+  g_autoptr(IdeLocation) end_location = NULL;
+  g_autoptr(IdeRange) range = NULL;
+  const gchar *new_text = NULL;
+  gboolean success;
+  struct {
+    gint64 line;
+    gint64 column;
+  } begin, end;
+
+  success = JSONRPC_MESSAGE_PARSE (text_edit,
+    "range", "{",
+      "start", "{",
+        "line", JSONRPC_MESSAGE_GET_INT64 (&begin.line),
+        "character", JSONRPC_MESSAGE_GET_INT64 (&begin.column),
+      "}",
+      "end", "{",
+        "line", JSONRPC_MESSAGE_GET_INT64 (&end.line),
+        "character", JSONRPC_MESSAGE_GET_INT64 (&end.column),
+      "}",
+    "}",
+    "newText", JSONRPC_MESSAGE_GET_STRING (&new_text)
+  );
+
+  if (!success)
+    {
+      IDE_TRACE_MSG ("Failed to extract text edit from variant");
+      return NULL;
+    }
+
+  begin_location = ide_location_new (gfile, begin.line, begin.column);
+  end_location = ide_location_new (gfile, end.line, end.column);
+  range = ide_range_new (begin_location, end_location);
+
+  return ide_text_edit_new (range, new_text);
+
+}
diff --git a/src/libide/lsp/ide-lsp-util.h b/src/libide/lsp/ide-lsp-util.h
index e36f56c33..09fda8d17 100644
--- a/src/libide/lsp/ide-lsp-util.h
+++ b/src/libide/lsp/ide-lsp-util.h
@@ -29,8 +29,11 @@
 G_BEGIN_DECLS
 
 IDE_AVAILABLE_IN_3_32
-IdeSymbolKind ide_lsp_decode_symbol_kind     (guint kind);
+IdeSymbolKind ide_lsp_decode_symbol_kind     (guint     kind);
 IDE_AVAILABLE_IN_3_32
-IdeSymbolKind ide_lsp_decode_completion_kind (guint kind);
+IdeSymbolKind ide_lsp_decode_completion_kind (guint     kind);
+IDE_AVAILABLE_IN_41
+IdeTextEdit  *ide_lsp_decode_text_edit       (GVariant *text_edit,
+                                              GFile    *gfile);
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]