[gnome-builder] plugin: codespell: Migrate to IdeDiagnosticTool



commit 59e9936d5716fd65392cbd188bc5bc19d14b8c3e
Author: Veli Tasalı <veli tasali gmail com>
Date:   Mon Feb 28 01:46:11 2022 +0300

    plugin: codespell: Migrate to IdeDiagnosticTool

 src/plugins/codespell/codespell-plugin.c           |   2 +-
 .../codespell/ide-codespell-diagnostic-provider.c  | 214 +++++----------------
 .../codespell/ide-codespell-diagnostic-provider.h  |   5 +-
 3 files changed, 48 insertions(+), 173 deletions(-)
---
diff --git a/src/plugins/codespell/codespell-plugin.c b/src/plugins/codespell/codespell-plugin.c
index c9e6c46a3..ec005c19e 100644
--- a/src/plugins/codespell/codespell-plugin.c
+++ b/src/plugins/codespell/codespell-plugin.c
@@ -21,7 +21,7 @@
 #include "config.h"
 
 #include <libpeas/peas.h>
-#include <libide-code.h>
+#include <libide-foundry.h>
 
 #include "ide-codespell-diagnostic-provider.h"
 
diff --git a/src/plugins/codespell/ide-codespell-diagnostic-provider.c 
b/src/plugins/codespell/ide-codespell-diagnostic-provider.c
index c35afc81a..e47678998 100644
--- a/src/plugins/codespell/ide-codespell-diagnostic-provider.c
+++ b/src/plugins/codespell/ide-codespell-diagnostic-provider.c
@@ -1,6 +1,7 @@
 /* ide-codespell-diagnostic-provider.c
  *
  * Copyright 2020 Günther Wagner <info gunibert de>
+ * Copyright 2022 Veli Tasalı <me velitasali com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,209 +19,82 @@
  * SPDX-License-Identifier: GPL-3.0-or-later
  */
 
+#include <glib/gi18n.h>
+
 #include "ide-codespell-diagnostic-provider.h"
 
 struct _IdeCodespellDiagnosticProvider
 {
-  IdeObject parent_instance;
-  char *codespell_path;
+  IdeDiagnosticTool parent_instance;
 };
 
-static void diagnostic_provider_iface_init (IdeDiagnosticProviderInterface *iface);
-
-G_DEFINE_FINAL_TYPE_WITH_CODE (IdeCodespellDiagnosticProvider,
-                         ide_codespell_diagnostic_provider,
-                         IDE_TYPE_OBJECT,
-                         G_IMPLEMENT_INTERFACE (IDE_TYPE_DIAGNOSTIC_PROVIDER,
-                                                diagnostic_provider_iface_init))
-
-IdeCodespellDiagnosticProvider *
-ide_codespell_diagnostic_provider_new (void)
-{
-  return g_object_new (IDE_TYPE_CODESPELL_DIAGNOSTIC_PROVIDER, NULL);
-}
-
-static void
-ide_codespell_diagnostic_provider_class_init (IdeCodespellDiagnosticProviderClass *klass)
-{
-}
+G_DEFINE_FINAL_TYPE (IdeCodespellDiagnosticProvider, ide_codespell_diagnostic_provider, 
IDE_TYPE_DIAGNOSTIC_TOOL)
 
 static void
-ide_codespell_diagnostic_provider_init (IdeCodespellDiagnosticProvider *self)
+ide_codespell_diagnostic_provider_configure_launcher (IdeDiagnosticTool     *tool,
+                                                      IdeSubprocessLauncher *launcher,
+                                                      GFile                 *file,
+                                                      GBytes                *contents)
 {
+  ide_subprocess_launcher_push_argv (launcher, "-");
 }
 
 static void
-ide_codespell_diagnostic_provider_communicate_cb (GObject      *object,
-                                                  GAsyncResult *result,
-                                                  gpointer      user_data)
+ide_codespell_diagnostic_provider_populate_diagnostics (IdeDiagnosticTool *tool,
+                                                        IdeDiagnostics    *diagnostics,
+                                                        GFile             *file,
+                                                        const char        *stdout_buf,
+                                                        const char        *stderr_buf)
 {
-  IdeSubprocess *subprocess = (IdeSubprocess *)object;
-  g_autoptr(IdeTask) task = user_data;
-  g_autoptr(IdeDiagnostics) ret = NULL;
-  g_autoptr(GError) error = NULL;
-  g_autofree gchar *stderr_buf = NULL;
-  g_autofree gchar *stdout_buf = NULL;
-  IdeLineReader reader;
-  GFile *file;
-  gchar *line;
-  gsize len;
-
-  g_assert (IDE_IS_SUBPROCESS (subprocess));
-  g_assert (G_IS_ASYNC_RESULT (result));
-  g_assert (IDE_IS_TASK (task));
-
-  if (!ide_subprocess_communicate_utf8_finish (subprocess, result, &stdout_buf, &stderr_buf, &error))
-    {
-      ide_task_return_error (task, g_steal_pointer (&error));
-      return;
-    }
-
-  file = ide_task_get_task_data (task);
-  g_assert (file != NULL);
-  g_assert (G_IS_FILE (file));
-
-  ret = ide_diagnostics_new ();
-
-  ide_line_reader_init (&reader, stdout_buf, -1);
-
-  while (NULL != (line = ide_line_reader_next (&reader, &len)))
+  g_autoptr(GRegex) regex = NULL;
+  g_autoptr(GError) regex_error = NULL;
+  g_autoptr(GMatchInfo) issues = NULL;
+
+  regex = g_regex_new ("(([0-9]+): .+?\n\t([a-zA-Z]+) ==> ([a-zA-Z0-9]+))",
+                       G_REGEX_RAW,
+                       G_REGEX_MATCH_NEWLINE_ANY,
+                       &regex_error);
+  g_regex_match (regex, stdout_buf, 0, &issues);
+  while (g_match_info_matches (issues))
     {
+      g_autofree gchar *line_word = g_match_info_fetch (issues, 2);
+      g_autofree gchar *typo_word = g_match_info_fetch (issues, 3);
+      g_autofree gchar *expected_word = g_match_info_fetch (issues, 4);
+      g_autofree gchar *diagnostic_text = NULL;
       g_autoptr(IdeDiagnostic) diag = NULL;
       g_autoptr(IdeLocation) loc = NULL;
       g_autoptr(IdeLocation) loc_end = NULL;
-      guint64 lineno;
-
-      line[len] = '\0';
-
-      /* Lines that we want to parse should look something like this:
-       * filename:42: misspelled word ==> correct word
-       */
-      if (!g_str_has_prefix (line, g_file_get_path (file)))
-        continue;
-
-      line += strlen (g_file_get_path (file)) + 1;
-      if (!g_ascii_isdigit (*line))
-        continue;
+      guint64 lineno = atoi (line_word);
 
-      lineno = g_ascii_strtoull (line, &line, 10);
-      if (lineno == G_MAXUINT64 || lineno == 0)
-        continue;
-      if (lineno > 0)
-        lineno--;
+      g_match_info_next (issues, NULL);
 
-      if (!g_str_has_prefix (line, ": "))
+      if (!lineno || !line_word || !typo_word || !expected_word)
         continue;
 
-      line += strlen (": ");
+      lineno--;
 
-      /* As we don't get a column information out of codespell mark the full line */
+      diagnostic_text = g_strdup_printf (_("Possible typo in '%s'. Did you mean '%s'?"),
+                                         typo_word,
+                                         expected_word);
       loc = ide_location_new (file, lineno, -1);
       loc_end = ide_location_new (file, lineno, G_MAXINT);
-      diag = ide_diagnostic_new (IDE_DIAGNOSTIC_NOTE, line, loc);
+      diag = ide_diagnostic_new (IDE_DIAGNOSTIC_NOTE, diagnostic_text, loc);
       ide_diagnostic_add_range (diag, ide_range_new (loc, loc_end));
-      ide_diagnostics_add (ret, diag);
+      ide_diagnostics_add (diagnostics, diag);
     }
-
-  ide_task_return_pointer (task,
-                           g_steal_pointer (&ret),
-                           g_object_unref);
-
 }
 
 static void
-ide_codespell_diagnostic_provider_diagnose_async (IdeDiagnosticProvider *provider,
-                                                  GFile                 *file,
-                                                  GBytes                *contents,
-                                                  const gchar           *lang_id,
-                                                  GCancellable          *cancellable,
-                                                  GAsyncReadyCallback    callback,
-                                                  gpointer               user_data)
-{
-  IdeCodespellDiagnosticProvider *self = (IdeCodespellDiagnosticProvider *)provider;
-  g_autoptr(IdeTask) task = NULL;
-  g_autoptr(IdeSubprocessLauncher) launcher = NULL;
-  g_autoptr(IdeSubprocess) subprocess = NULL;
-  g_autoptr(GError) error = NULL;
-
-  g_assert (IDE_IS_CODESPELL_DIAGNOSTIC_PROVIDER (self));
-  g_assert (G_IS_FILE (file));
-  g_assert (contents != NULL);
-  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
-
-  task = ide_task_new (self, cancellable, callback, user_data);
-  ide_task_set_source_tag (task, ide_codespell_diagnostic_provider_diagnose_async);
-  ide_task_set_priority (task, G_PRIORITY_LOW);
-  ide_task_set_task_data (task, g_object_ref (file), g_object_unref);
-
-  if (self->codespell_path == NULL)
-    {
-      ide_task_return_new_error (task,
-                                 G_IO_ERROR,
-                                 G_IO_ERROR_NOT_SUPPORTED,
-                                 "Not supported");
-      return;
-    }
-
-  launcher = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE |
-                                          G_SUBPROCESS_FLAGS_STDOUT_PIPE |
-                                          G_SUBPROCESS_FLAGS_STDERR_SILENCE);
-
-  ide_subprocess_launcher_push_argv (launcher, "codespell");
-  /* ide_subprocess_launcher_push_argv (launcher, "-d"); */
-  ide_subprocess_launcher_push_argv (launcher, g_file_get_path (file));
-
-  /* Spawn the process of fail immediately */
-  if (!(subprocess = ide_subprocess_launcher_spawn (launcher, cancellable, &error)))
-    {
-      ide_task_return_error (task, g_steal_pointer (&error));
-      return;
-    }
-
-  ide_subprocess_communicate_utf8_async (subprocess,
-                                         NULL,
-                                         cancellable,
-                                         ide_codespell_diagnostic_provider_communicate_cb,
-                                         g_steal_pointer (&task));
-}
-
-static IdeDiagnostics *
-ide_codespell_diagnostic_provider_diagnose_finish (IdeDiagnosticProvider  *provider,
-                                                   GAsyncResult           *result,
-                                                   GError                **error)
-{
-  g_assert (IDE_IS_CODESPELL_DIAGNOSTIC_PROVIDER (provider));
-  g_assert (IDE_IS_TASK (result));
-  g_assert (ide_task_is_valid (IDE_TASK (result), provider));
-
-  return ide_task_propagate_pointer (IDE_TASK (result), error);
-}
-
-static void
-ide_codespell_diagnostic_provider_load (IdeDiagnosticProvider *provider)
-{
-  IdeCodespellDiagnosticProvider *self = (IdeCodespellDiagnosticProvider *)provider;
-
-  g_assert (IDE_IS_CODESPELL_DIAGNOSTIC_PROVIDER (self));
-
-  self->codespell_path = g_find_program_in_path ("codespell");
-}
-
-static void
-ide_codespell_diagnostic_provider_unload (IdeDiagnosticProvider *provider)
+ide_codespell_diagnostic_provider_class_init (IdeCodespellDiagnosticProviderClass *klass)
 {
-  IdeCodespellDiagnosticProvider *self = (IdeCodespellDiagnosticProvider *)provider;
-
-  g_assert (IDE_IS_CODESPELL_DIAGNOSTIC_PROVIDER (self));
+  IdeDiagnosticToolClass *tool_class = IDE_DIAGNOSTIC_TOOL_CLASS (klass);
 
-  g_clear_pointer (&self->codespell_path, g_free);
+  tool_class->configure_launcher = ide_codespell_diagnostic_provider_configure_launcher;
+  tool_class->populate_diagnostics = ide_codespell_diagnostic_provider_populate_diagnostics;
 }
 
 static void
-diagnostic_provider_iface_init (IdeDiagnosticProviderInterface *iface)
+ide_codespell_diagnostic_provider_init (IdeCodespellDiagnosticProvider *self)
 {
-  iface->diagnose_async = ide_codespell_diagnostic_provider_diagnose_async;
-  iface->diagnose_finish = ide_codespell_diagnostic_provider_diagnose_finish;
-  iface->load = ide_codespell_diagnostic_provider_load;
-  iface->unload = ide_codespell_diagnostic_provider_unload;
+  ide_diagnostic_tool_set_program_name (IDE_DIAGNOSTIC_TOOL (self), "codespell");
 }
diff --git a/src/plugins/codespell/ide-codespell-diagnostic-provider.h 
b/src/plugins/codespell/ide-codespell-diagnostic-provider.h
index 1271abcdb..0672c0cb5 100644
--- a/src/plugins/codespell/ide-codespell-diagnostic-provider.h
+++ b/src/plugins/codespell/ide-codespell-diagnostic-provider.h
@@ -1,6 +1,7 @@
 /* ide-codespell-diagnostic-provider.h
  *
  * Copyright 2020 Günther Wagner <info gunibert de>
+ * Copyright 2022 Veli Tasalı <me velitasali com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,12 +21,12 @@
 
 #pragma once
 
-#include <libide-code.h>
+#include <libide-foundry.h>
 
 G_BEGIN_DECLS
 
 #define IDE_TYPE_CODESPELL_DIAGNOSTIC_PROVIDER (ide_codespell_diagnostic_provider_get_type())
 
-G_DECLARE_FINAL_TYPE (IdeCodespellDiagnosticProvider, ide_codespell_diagnostic_provider, IDE, 
CODESPELL_DIAGNOSTIC_PROVIDER, IdeObject)
+G_DECLARE_FINAL_TYPE (IdeCodespellDiagnosticProvider, ide_codespell_diagnostic_provider, IDE, 
CODESPELL_DIAGNOSTIC_PROVIDER, IdeDiagnosticTool)
 
 G_END_DECLS


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