From 321eb93a875753c17b560f194c8323fc6d484873 Mon Sep 17 00:00:00 2001
From: ccagml <ccagml@163.com>
Date: Wed, 7 Dec 2022 19:12:32 +0800
Subject: [PATCH] update

---
 CHANGELOG.md                       |  4 ++
 package.json                       | 73 +++++++++++++++++++++++++++++-
 src/controller/RemarkController.ts |  3 ++
 src/extension.ts                   |  3 ++
 src/service/FileButtonService.ts   |  5 ++
 src/service/RemarkService.ts       | 22 +++++++++
 src/utils/ConfigUtils.ts           | 13 ++++++
 7 files changed, 122 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index c313053..912f7bb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+## version 2.8.1
+
+- 插入头文件,解决避免都是红色波浪线语法提示
+
 ## version 2.7.3
 
 - 某些情况下颜色对比显示错误
diff --git a/package.json b/package.json
index 3aba4e9..e811e4f 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
     "name": "vscode-leetcode-problem-rating",
     "displayName": "LeetCode",
     "description": "LeetCode 官方插件增强, 代码开源, 增加 LeetCode 题目难度分, 给个star吧, 球球了",
-    "version": "2.7.3",
+    "version": "2.8.1",
     "author": "ccagml",
     "publisher": "ccagml",
     "license": "MIT",
@@ -242,6 +242,11 @@
                 "command": "lcpr.removeQidFromGroup",
                 "title": "从分类中移除这个题目",
                 "icon": "$(remove)"
+            },
+            {
+                "command": "lcpr.includeTemplates",
+                "title": "插入头文件模板",
+                "icon": "$(remove)"
             }
         ],
         "viewsContainers": {
@@ -568,6 +573,72 @@
             {
                 "title": "leetcode-problem-rating",
                 "properties": {
+                    "leetcode-problem-rating.includeTemplates": {
+                        "type": "array",
+                        "description": "引入一些默认内容,防止vscode都是波浪红线",
+                        "default": [
+                            {
+                                "language": "cpp",
+                                "template": [
+                                    "using namespace std;",
+                                    "#include <algorithm>",
+                                    "#include <array>",
+                                    "#include <bitset>",
+                                    "#include <climits>",
+                                    "#include <deque>",
+                                    "#include <functional>",
+                                    "#include <iostream>",
+                                    "#include <list>",
+                                    "#include <queue>",
+                                    "#include <stack>",
+                                    "#include <tuple>",
+                                    "#include <unordered_map>",
+                                    "#include <unordered_set>",
+                                    "#include <utility>",
+                                    "#include <vector>"
+                                ]
+                            }
+                        ],
+                        "items": {
+                            "type": "object",
+                            "required": [
+                                "language"
+                            ],
+                            "properties": {
+                                "language": {
+                                    "type": "string",
+                                    "enum": [
+                                        "bash",
+                                        "c",
+                                        "cpp",
+                                        "csharp",
+                                        "golang",
+                                        "java",
+                                        "javascript",
+                                        "kotlin",
+                                        "mysql",
+                                        "php",
+                                        "python",
+                                        "python3",
+                                        "ruby",
+                                        "rust",
+                                        "scala",
+                                        "swift",
+                                        "typescript"
+                                    ],
+                                    "description": "哪种语言"
+                                },
+                                "template": {
+                                    "type": "array",
+                                    "default": [],
+                                    "description": "模板内容",
+                                    "items": {
+                                        "type": "string"
+                                    }
+                                }
+                            }
+                        }
+                    },
                     "leetcode-problem-rating.hideSolved": {
                         "type": "boolean",
                         "default": false,
diff --git a/src/controller/RemarkController.ts b/src/controller/RemarkController.ts
index 91e447a..55f1d12 100644
--- a/src/controller/RemarkController.ts
+++ b/src/controller/RemarkController.ts
@@ -33,6 +33,9 @@ class RemarkController implements Disposable {
   public async startRemark(document: TextDocument) {
     await remarkService.startRemark(document);
   }
+  public async includeTemplates(document: TextDocument) {
+    await remarkService.includeTemplates(document);
+  }
 
   public remarkCancelsaveNote(comment: RemarkComment) {
     remarkService.remarkCancelsaveNote(comment);
diff --git a/src/extension.ts b/src/extension.ts
index 33499fb..f4e480d 100644
--- a/src/extension.ts
+++ b/src/extension.ts
@@ -133,6 +133,9 @@ export async function activate(context: ExtensionContext): Promise<void> {
       }),
       commands.registerCommand("lcpr.startRemark", (document: TextDocument) => {
         remarkController.startRemark(document);
+      }),
+      commands.registerCommand("lcpr.includeTemplates", (document: TextDocument) => {
+        remarkController.includeTemplates(document);
       })
     );
 
diff --git a/src/service/FileButtonService.ts b/src/service/FileButtonService.ts
index 7e5edea..88e974f 100644
--- a/src/service/FileButtonService.ts
+++ b/src/service/FileButtonService.ts
@@ -147,6 +147,11 @@ export class FileButtonService implements vscode.CodeLensProvider {
         title: "remark",
         command: "lcpr.startRemark",
         arguments: [document],
+      }),
+      new vscode.CodeLens(range, {
+        title: "includeTemplates",
+        command: "lcpr.includeTemplates",
+        arguments: [document],
       })
     );
     return temp_result;
diff --git a/src/service/RemarkService.ts b/src/service/RemarkService.ts
index d57e685..fce1311 100644
--- a/src/service/RemarkService.ts
+++ b/src/service/RemarkService.ts
@@ -8,10 +8,13 @@ import {
   comments,
   Range,
   Disposable,
+  window,
+  Position,
 } from "vscode";
 import { treeViewController } from "../controller/TreeViewController";
 import { remarkDao } from "../dao/remarkDao";
 import { RemarkComment } from "../model/Model";
+import { getIncludeTemplate } from "../utils/ConfigUtils";
 
 class RemarkService implements Disposable {
   private _remarkComment;
@@ -44,6 +47,25 @@ class RemarkService implements Disposable {
     };
   }
 
+  public async includeTemplates(document: TextDocument) {
+    const content: string = document.getText();
+    const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=(.*)/);
+    if (!matchResult || !matchResult[2]) {
+      return undefined;
+    }
+    for (let i: number = 0; i < document.lineCount; i++) {
+      const lineContent: string = document.lineAt(i).text;
+
+      if (lineContent.indexOf("@lc code=start") >= 0) {
+        const editor = window.activeTextEditor;
+        editor?.edit((edit) => {
+          edit.insert(new Position(i - 1, i - 1), getIncludeTemplate(matchResult[2]));
+        });
+      }
+    }
+    return undefined;
+  }
+
   public async startRemark(document: TextDocument) {
     let docInfo = this.getQidByDocument(document);
     if (docInfo["qid"] == undefined) {
diff --git a/src/utils/ConfigUtils.ts b/src/utils/ConfigUtils.ts
index 384836c..69ba26b 100644
--- a/src/utils/ConfigUtils.ts
+++ b/src/utils/ConfigUtils.ts
@@ -370,3 +370,16 @@ export async function setDefaultLanguage(): Promise<void> {
 export function isAnswerDiffColor(): boolean {
   return getVsCodeConfig().get<boolean>("answerDiffColor", false);
 }
+
+export function getIncludeTemplate(lang: string): string {
+  let temp_cfg = getVsCodeConfig().get<any>("includeTemplates") || [];
+  let result = "";
+  temp_cfg.forEach((element) => {
+    if (element.language == lang) {
+      result = (element.template || []).join("\n");
+      return;
+    }
+  });
+
+  return result;
+}