linthis 0.22.0

A fast, cross-platform multi-language linter and formatter
Documentation
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Copyright 2024 zhlinh and linthis Project Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found at
//
// https://opensource.org/license/MIT
//
// The above copyright notice and this permission
// notice shall be included in all copies or
// substantial portions of the Software.

//! Prompt templates for AI-assisted fix suggestions.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Categories of lint issues for specialized prompts
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IssueCategory {
    /// Code style issues (formatting, naming)
    Style,
    /// Security vulnerabilities
    Security,
    /// Performance problems
    Performance,
    /// Code complexity issues
    Complexity,
    /// Bug patterns and potential errors
    Bug,
    /// Deprecated API usage
    Deprecation,
    /// Type-related issues
    Type,
    /// Documentation issues
    Documentation,
    /// Best practices
    BestPractice,
    /// General/unknown category
    #[default]
    General,
}

impl std::str::FromStr for IssueCategory {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "style" | "formatting" | "naming" => Ok(Self::Style),
            "security" | "vulnerability" | "vuln" => Ok(Self::Security),
            "performance" | "perf" | "speed" => Ok(Self::Performance),
            "complexity" | "cyclomatic" | "cognitive" => Ok(Self::Complexity),
            "bug" | "error" | "defect" => Ok(Self::Bug),
            "deprecation" | "deprecated" => Ok(Self::Deprecation),
            "type" | "typing" => Ok(Self::Type),
            "documentation" | "doc" | "docs" => Ok(Self::Documentation),
            "best-practice" | "bestpractice" | "practice" => Ok(Self::BestPractice),
            _ => Ok(Self::General),
        }
    }
}

/// Template for generating AI prompts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptTemplate {
    /// Template name
    pub name: String,
    /// Issue category this template is for
    pub category: IssueCategory,
    /// System prompt for the AI
    pub system_prompt: String,
    /// User prompt template with placeholders
    pub user_prompt_template: String,
    /// Additional context instructions
    pub context_instructions: Option<String>,
}

impl PromptTemplate {
    /// Create a new prompt template
    pub fn new(name: &str, category: IssueCategory, system: &str, template: &str) -> Self {
        Self {
            name: name.to_string(),
            category,
            system_prompt: system.to_string(),
            user_prompt_template: template.to_string(),
            context_instructions: None,
        }
    }

    /// Add context instructions
    pub fn with_context_instructions(mut self, instructions: &str) -> Self {
        self.context_instructions = Some(instructions.to_string());
        self
    }
}

/// Builder for constructing AI prompts
pub struct PromptBuilder {
    templates: HashMap<IssueCategory, PromptTemplate>,
    default_template: PromptTemplate,
}

impl Default for PromptBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl PromptBuilder {
    /// Create a new prompt builder with default templates
    pub fn new() -> Self {
        let mut templates = HashMap::new();

        // Style template
        templates.insert(
            IssueCategory::Style,
            PromptTemplate::new(
                "style_fix",
                IssueCategory::Style,
                SYSTEM_PROMPT_STYLE,
                USER_PROMPT_STYLE,
            ),
        );

        // Security template
        templates.insert(
            IssueCategory::Security,
            PromptTemplate::new(
                "security_fix",
                IssueCategory::Security,
                SYSTEM_PROMPT_SECURITY,
                USER_PROMPT_SECURITY,
            ),
        );

        // Performance template
        templates.insert(
            IssueCategory::Performance,
            PromptTemplate::new(
                "performance_fix",
                IssueCategory::Performance,
                SYSTEM_PROMPT_PERFORMANCE,
                USER_PROMPT_PERFORMANCE,
            ),
        );

        // Complexity template
        templates.insert(
            IssueCategory::Complexity,
            PromptTemplate::new(
                "complexity_fix",
                IssueCategory::Complexity,
                SYSTEM_PROMPT_COMPLEXITY,
                USER_PROMPT_COMPLEXITY,
            ),
        );

        // Bug template
        templates.insert(
            IssueCategory::Bug,
            PromptTemplate::new(
                "bug_fix",
                IssueCategory::Bug,
                SYSTEM_PROMPT_BUG,
                USER_PROMPT_BUG,
            ),
        );

        // Default/general template
        let default_template = PromptTemplate::new(
            "general_fix",
            IssueCategory::General,
            SYSTEM_PROMPT_GENERAL,
            USER_PROMPT_GENERAL,
        );

        Self {
            templates,
            default_template,
        }
    }

    /// Get template for a specific category
    pub fn get_template(&self, category: IssueCategory) -> &PromptTemplate {
        self.templates
            .get(&category)
            .unwrap_or(&self.default_template)
    }

    /// Build a prompt for an issue
    pub fn build_prompt(
        &self,
        category: IssueCategory,
        variables: &PromptVariables,
    ) -> (String, String) {
        let template = self.get_template(category);

        let system = self.substitute_variables(&template.system_prompt, variables);
        let user = self.substitute_variables(&template.user_prompt_template, variables);

        (system, user)
    }

    /// Substitute variables in a template string
    fn substitute_variables(&self, template: &str, vars: &PromptVariables) -> String {
        let mut result = template.to_string();

        result = result.replace("{{language}}", &vars.language);
        result = result.replace("{{file_path}}", &vars.file_path);
        result = result.replace("{{line_number}}", &vars.line_number.to_string());
        result = result.replace("{{issue_message}}", &vars.issue_message);
        result = result.replace("{{rule_id}}", &vars.rule_id);
        result = result.replace("{{code_context}}", &vars.code_context);
        result = result.replace("{{issue_line}}", &vars.issue_line);

        if let Some(ref imports) = vars.imports {
            result = result.replace("{{imports}}", imports);
        } else {
            result = result.replace("{{imports}}", "");
        }

        if let Some(ref scope) = vars.scope {
            result = result.replace("{{scope}}", scope);
        } else {
            result = result.replace("{{scope}}", "");
        }

        result
    }

    /// Add or override a template
    pub fn add_template(&mut self, template: PromptTemplate) {
        self.templates.insert(template.category, template);
    }
}

/// Variables for prompt substitution
#[derive(Debug, Clone, Default)]
pub struct PromptVariables {
    /// Programming language
    pub language: String,
    /// File path
    pub file_path: String,
    /// Line number of the issue
    pub line_number: u32,
    /// Lint issue message
    pub issue_message: String,
    /// Rule ID/code
    pub rule_id: String,
    /// Code context around the issue
    pub code_context: String,
    /// The specific line with the issue
    pub issue_line: String,
    /// Import statements (if available)
    pub imports: Option<String>,
    /// Enclosing scope (function/class)
    pub scope: Option<String>,
}

impl PromptVariables {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_language(mut self, lang: &str) -> Self {
        self.language = lang.to_string();
        self
    }

    pub fn with_file_path(mut self, path: &str) -> Self {
        self.file_path = path.to_string();
        self
    }

    pub fn with_line_number(mut self, line: u32) -> Self {
        self.line_number = line;
        self
    }

    pub fn with_issue_message(mut self, msg: &str) -> Self {
        self.issue_message = msg.to_string();
        self
    }

    pub fn with_rule_id(mut self, rule: &str) -> Self {
        self.rule_id = rule.to_string();
        self
    }

    pub fn with_code_context(mut self, context: &str) -> Self {
        self.code_context = context.to_string();
        self
    }

    pub fn with_issue_line(mut self, line: &str) -> Self {
        self.issue_line = line.to_string();
        self
    }

    pub fn with_imports(mut self, imports: &str) -> Self {
        self.imports = Some(imports.to_string());
        self
    }

    pub fn with_scope(mut self, scope: &str) -> Self {
        self.scope = Some(scope.to_string());
        self
    }
}

// System prompts

const SYSTEM_PROMPT_GENERAL: &str = r#"You are an expert code reviewer and fix assistant. Your task is to analyze lint issues and provide precise, minimal fixes.

CRITICAL: Always provide your fix as a unified diff. This ensures accurate application of changes.

Response format - use unified diff:
```diff
@@ -LINE_NUM,COUNT +LINE_NUM,COUNT @@
 context line (unchanged, starts with space)
-removed line (starts with minus)
+added line (starts with plus)
 context line (unchanged, starts with space)
```

Guidelines:
1. Make MINIMAL changes - fix ONLY what the error message describes
2. Preserve original code style, formatting, and indentation
3. Include 1 line of context before and after the change
4. Do NOT include the entire file - only the changed section
5. LINE_NUM is the line number where the change starts
6. COUNT is the number of lines in that section

Common lint rules and their fixes:
- "Missing space after X" → Add a space AFTER the character X
- "Missing space before X" → Add a space BEFORE the character X
- "Extra space" → Remove the extra space
- "Line too long" → Break the line appropriately
- "Unused variable" → Remove or prefix with underscore

Example - fixing "unused variable x" on line 5:
```diff
@@ -4,3 +4,3 @@
     let y = 10;
-    let x = 5;
+    let _x = 5;
     println!("{}", y);
```"#;

const SYSTEM_PROMPT_STYLE: &str = r#"You are an expert code formatter and style guide enforcer. Your task is to fix code style issues while preserving functionality.

CRITICAL: Always provide your fix as a unified diff. This ensures accurate application of changes.

Response format - use unified diff:
```diff
@@ -LINE_NUM,COUNT +LINE_NUM,COUNT @@
 context line (unchanged, starts with space)
-removed line (starts with minus)
+added line (starts with plus)
 context line (unchanged, starts with space)
```

Guidelines:
1. Follow language-specific style conventions
2. Make MINIMAL changes - fix ONLY what the error message describes
3. Preserve existing formatting patterns where not explicitly wrong
4. Do NOT change indentation unless the error specifically mentions indentation
5. Include 1 line of context before and after the change
6. Do NOT include the entire file

Common style rules and their fixes:
- "Missing space after X" → Add a space AFTER the character X
- "Missing space before X" → Add a space BEFORE the character X
- "Extra space after X" → Remove the extra space after X
- "Line too long" → Break the line appropriately
- "Trailing whitespace" → Remove spaces/tabs at the end of line"#;

const SYSTEM_PROMPT_SECURITY: &str = r#"You are a security expert. Your task is to fix security vulnerabilities in code while maintaining functionality.

CRITICAL: Always provide your fix as a unified diff:
```diff
@@ -LINE_NUM,COUNT +LINE_NUM,COUNT @@
 context line (unchanged)
-removed line
+added line
```

Guidelines:
1. Apply security best practices
2. Use secure alternatives to vulnerable patterns
3. Add input validation where needed
4. Include 1-2 lines of context before and after
5. Do NOT include the entire file

Security note: Add brief explanation after the diff if needed"#;

const SYSTEM_PROMPT_PERFORMANCE: &str = r#"You are a performance optimization expert. Your task is to fix performance issues while maintaining code correctness.

CRITICAL: Always provide your fix as a unified diff:
```diff
@@ -LINE_NUM,COUNT +LINE_NUM,COUNT @@
 context line (unchanged)
-removed line
+added line
```

Guidelines:
1. Optimize only what's flagged as a performance issue
2. Prefer clarity over micro-optimizations
3. Include 1-2 lines of context before and after
4. Do NOT include the entire file"#;

const SYSTEM_PROMPT_COMPLEXITY: &str = r#"You are a code simplification expert. Your task is to reduce code complexity while maintaining functionality.

CRITICAL: Always provide your fix as a unified diff:
```diff
@@ -LINE_NUM,COUNT +LINE_NUM,COUNT @@
 context line (unchanged)
-removed line
+added line
```

Guidelines:
1. Simplify conditional logic or reduce nesting
2. Maintain the original behavior exactly
3. Include context lines before and after
4. Do NOT include the entire file"#;

const SYSTEM_PROMPT_BUG: &str = r#"You are a debugging expert. Your task is to fix potential bugs and error patterns in code.

CRITICAL: Always provide your fix as a unified diff:
```diff
@@ -LINE_NUM,COUNT +LINE_NUM,COUNT @@
 context line (unchanged)
-removed line
+added line
```

Guidelines:
1. Fix the specific bug pattern identified
2. Consider edge cases
3. Include 1-2 lines of context before and after
4. Do NOT include the entire file

Bug fix note: Add brief explanation after the diff if needed"#;

// User prompts

const USER_PROMPT_GENERAL: &str = r#"Fix the following lint issue in {{language}} code:

File: {{file_path}}
Line: {{line_number}}
Issue: {{issue_message}}
Rule: {{rule_id}}

Code context:
```{{language}}
{{code_context}}
```

The issue is on this line (line {{line_number}}):
```{{language}}
{{issue_line}}
```

IMPORTANT:
- The error message "{{issue_message}}" tells you EXACTLY what to fix
- Fix ONLY what the error describes, do NOT change other lines
- Preserve original indentation and formatting

Provide your fix as a unified diff starting at line {{line_number}}:
```diff
@@ -{{line_number}},N +{{line_number}},M @@
 context line before
-original problematic line
+fixed line
 context line after
```"#;

const USER_PROMPT_STYLE: &str = r#"Fix the following code style issue in {{language}}:

File: {{file_path}}
Line: {{line_number}}
Style Issue: {{issue_message}}
Rule: {{rule_id}}

Code context:
```{{language}}
{{code_context}}
```

Problem line (line {{line_number}}):
```{{language}}
{{issue_line}}
```

IMPORTANT:
- "{{issue_message}}" tells you EXACTLY what to fix
- Make the MINIMAL change to fix ONLY this specific issue
- Do NOT change other lines or formatting

Provide your fix as a unified diff:
```diff
@@ -{{line_number}},1 +{{line_number}},1 @@
-{{issue_line}}
+fixed line here
```"#;

const USER_PROMPT_SECURITY: &str = r#"Fix the following security vulnerability in {{language}}:

File: {{file_path}}
Line: {{line_number}}
Security Issue: {{issue_message}}
Rule: {{rule_id}}

Vulnerable code:
```{{language}}
{{code_context}}
```

Problem line (line {{line_number}}):
```{{language}}
{{issue_line}}
```

Provide your fix as a unified diff:
```diff
@@ -{{line_number}},N +{{line_number}},M @@
-vulnerable code
+secure code
```"#;

const USER_PROMPT_PERFORMANCE: &str = r#"Optimize the following performance issue in {{language}}:

File: {{file_path}}
Line: {{line_number}}
Performance Issue: {{issue_message}}
Rule: {{rule_id}}

Code to optimize:
```{{language}}
{{code_context}}
```

Problem area (line {{line_number}}):
```{{language}}
{{issue_line}}
```

Provide your fix as a unified diff:
```diff
@@ -{{line_number}},N +{{line_number}},M @@
-slow code
+optimized code
```"#;

const USER_PROMPT_COMPLEXITY: &str = r#"Simplify the following complex code in {{language}}:

File: {{file_path}}
Line: {{line_number}}
Complexity Issue: {{issue_message}}
Rule: {{rule_id}}

Complex code:
```{{language}}
{{code_context}}
```

{{#if scope}}
Full function/method:
```{{language}}
{{scope}}
```
{{/if}}

Provide your fix as a unified diff:
```diff
@@ -{{line_number}},N +{{line_number}},M @@
-complex code
+simplified code
```"#;

const USER_PROMPT_BUG: &str = r#"Fix the following potential bug in {{language}}:

File: {{file_path}}
Line: {{line_number}}
Bug Pattern: {{issue_message}}
Rule: {{rule_id}}

Buggy code:
```{{language}}
{{code_context}}
```

Problem line (line {{line_number}}):
```{{language}}
{{issue_line}}
```

Provide your fix as a unified diff:
```diff
@@ -{{line_number}},N +{{line_number}},M @@
-buggy code
+fixed code
```"#;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_issue_category_parsing() {
        assert_eq!(
            "style".parse::<IssueCategory>().unwrap(),
            IssueCategory::Style
        );
        assert_eq!(
            "security".parse::<IssueCategory>().unwrap(),
            IssueCategory::Security
        );
        assert_eq!(
            "performance".parse::<IssueCategory>().unwrap(),
            IssueCategory::Performance
        );
        assert_eq!(
            "unknown".parse::<IssueCategory>().unwrap(),
            IssueCategory::General
        );
    }

    #[test]
    fn test_prompt_builder() {
        let builder = PromptBuilder::new();

        let vars = PromptVariables::new()
            .with_language("rust")
            .with_file_path("src/main.rs")
            .with_line_number(10)
            .with_issue_message("unused variable")
            .with_rule_id("W0001")
            .with_code_context("let x = 5;")
            .with_issue_line("let x = 5;");

        let (system, user) = builder.build_prompt(IssueCategory::General, &vars);

        assert!(system.contains("expert code reviewer"));
        assert!(user.contains("rust"));
        assert!(user.contains("src/main.rs"));
        assert!(user.contains("unused variable"));
    }

    #[test]
    fn test_prompt_template() {
        let template =
            PromptTemplate::new("test", IssueCategory::Style, "System prompt", "User prompt");

        assert_eq!(template.name, "test");
        assert_eq!(template.category, IssueCategory::Style);
    }

    #[test]
    fn test_category_specific_templates() {
        let builder = PromptBuilder::new();

        let security = builder.get_template(IssueCategory::Security);
        assert!(security.system_prompt.contains("security"));

        let performance = builder.get_template(IssueCategory::Performance);
        assert!(performance.system_prompt.contains("performance"));
    }
}