File: plugin-codespell-diagnostic-tool.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (171 lines) | stat: -rw-r--r-- 7,014 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* plugin-codespell-diagnostic-tool.c
 *
 * Copyright 2025 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include "plugin-codespell-diagnostic-tool.h"

struct _PluginCodespellDiagnosticTool
{
  FoundryDiagnosticTool parent_instance;
};

G_DEFINE_FINAL_TYPE (PluginCodespellDiagnosticTool, plugin_codespell_diagnostic_tool, FOUNDRY_TYPE_DIAGNOSTIC_TOOL)

static DexFuture *
plugin_codespell_diagnostic_tool_prepare (FoundryDiagnosticTool  *tool,
                                          FoundryProcessLauncher *launcher,
                                          const char * const     *argv,
                                          const char * const     *environ,
                                          const char             *language)
{
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GError) error = NULL;
  g_autoptr(GFile) state_directory = NULL;
  g_autoptr(GFile) project_ignore = NULL;
  g_autoptr(GFile) user_ignore = NULL;
  g_autofree char *ignore_param = NULL;

  if (!dex_await (FOUNDRY_DIAGNOSTIC_TOOL_CLASS (plugin_codespell_diagnostic_tool_parent_class)->prepare (tool, launcher, argv, environ, language), &error))
    return dex_future_new_for_error (g_steal_pointer (&error));

  context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (tool));
  state_directory = foundry_context_dup_state_directory (context);
  project_ignore = g_file_get_child (state_directory, "project/codespell-ignore.txt");
  user_ignore = g_file_get_child (state_directory, "user/codespell-ignore.txt");

  if (dex_await_boolean (dex_file_query_exists (project_ignore), NULL))
    foundry_process_launcher_append_args (launcher, FOUNDRY_STRV_INIT ("-I", g_file_peek_path (project_ignore)));

  if (dex_await_boolean (dex_file_query_exists (user_ignore), NULL))
    foundry_process_launcher_append_args (launcher, FOUNDRY_STRV_INIT ("-I", g_file_peek_path (user_ignore)));

  return dex_future_new_true ();
}

static DexFuture *
plugin_codespell_diagnostic_tool_dup_bytes_for_stdin (FoundryDiagnosticTool *diagnostic_tool,
                                                      GFile                 *file,
                                                      GBytes                *contents,
                                                      const char            *laguage)
{
  g_assert (PLUGIN_IS_CODESPELL_DIAGNOSTIC_TOOL (diagnostic_tool));
  g_assert (!file || G_IS_FILE (file));
  g_assert (file || contents);

  if (contents == NULL)
    return dex_file_load_contents_bytes (file);
  else
    return dex_future_new_take_boxed (G_TYPE_BYTES, g_bytes_ref (contents));
}

static DexFuture *
plugin_codespell_diagnostic_tool_extract_from_stdout (FoundryDiagnosticTool *diagnostic_tool,
                                                      GFile                 *file,
                                                      GBytes                *contents,
                                                      const char            *language,
                                                      GBytes                *stdout_bytes)
{
  static GRegex *regex;
  g_autoptr(FoundryDiagnosticBuilder) builder = NULL;
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GListStore) diagnostics = NULL;
  g_autoptr(GMatchInfo) issues = NULL;
  const char *data;
  gsize len;

  g_assert (PLUGIN_IS_CODESPELL_DIAGNOSTIC_TOOL (diagnostic_tool));
  g_assert (!file || G_IS_FILE (file));
  g_assert (file || contents);
  g_assert (stdout_bytes);

  if G_UNLIKELY (regex == NULL)
    {
      g_autoptr(GError) error = NULL;
      regex = g_regex_new ("(([0-9]+): .+?\n\t([a-zA-Z]+) ==> ([a-zA-Z0-9]+))",
                           G_REGEX_RAW,
                           G_REGEX_MATCH_NEWLINE_ANY,
                           &error);
      if (error != NULL)
        return dex_future_new_for_error (g_steal_pointer (&error));
    }

  context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (diagnostic_tool));
  builder = foundry_diagnostic_builder_new (context);
  diagnostics = g_list_store_new (FOUNDRY_TYPE_DIAGNOSTIC);

  if (!(data = g_bytes_get_data (stdout_bytes, &len)) || !len)
    return dex_future_new_take_object (g_steal_pointer (&diagnostics));

  if (!g_regex_match_full (regex, data, len, 0, 0, &issues, NULL))
    return dex_future_new_take_object (g_steal_pointer (&diagnostics));

  g_assert (issues != NULL);

  while (g_match_info_matches (issues))
    {
      g_autofree char *line_word = g_match_info_fetch (issues, 2);
      g_autofree char *typo_word = g_match_info_fetch (issues, 3);
      g_autofree char *expected_word = g_match_info_fetch (issues, 4);
      guint64 lineno = g_ascii_strtoull (line_word, NULL, 10);

      if (lineno != 0 &&
          line_word != NULL &&
          typo_word != NULL &&
          expected_word != NULL)
        {
          g_autoptr(FoundryDiagnostic) diagnostic = NULL;

          foundry_diagnostic_builder_set_file (builder, file);
          foundry_diagnostic_builder_set_line (builder, lineno);
          foundry_diagnostic_builder_take_message (builder,
                                                   g_strdup_printf (_("Possible typo in ā€œ%sā€. Did you mean ā€œ%sā€?"),
                                                                    typo_word, expected_word));

          diagnostic = foundry_diagnostic_builder_end (builder);

          g_list_store_append (diagnostics, diagnostic);
        }

      if (!g_match_info_next (issues, NULL))
        break;
    }

  return dex_future_new_take_object (g_steal_pointer (&diagnostics));
}

static void
plugin_codespell_diagnostic_tool_class_init (PluginCodespellDiagnosticToolClass *klass)
{
  FoundryDiagnosticToolClass *diagnostic_tool_class = FOUNDRY_DIAGNOSTIC_TOOL_CLASS (klass);

  diagnostic_tool_class->dup_bytes_for_stdin = plugin_codespell_diagnostic_tool_dup_bytes_for_stdin;
  diagnostic_tool_class->extract_from_stdout = plugin_codespell_diagnostic_tool_extract_from_stdout;
  diagnostic_tool_class->prepare = plugin_codespell_diagnostic_tool_prepare;
}

static void
plugin_codespell_diagnostic_tool_init (PluginCodespellDiagnosticTool *self)
{
  foundry_diagnostic_tool_set_argv (FOUNDRY_DIAGNOSTIC_TOOL (self),
                                    FOUNDRY_STRV_INIT ("codespell", "-"));
}