# vscode.easy-include **Repository Path**: zinface/vscode.easy-include ## Basic Information - **Project Name**: vscode.easy-include - **Description**: fork https://github.com/soehrl/easy-include - **Primary Language**: TypeScript - **License**: Not specified - **Default Branch**: master - **Homepage**: https://github.com/soehrl/easy-include - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-14 - **Last Updated**: 2023-02-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: vscode插件 ## README # easy-include README Add an include statement to the top of the file from anywhere ## Features * `easyinclude.addInclude`: Adds an include statement to the current file ## Release Notes ### 0.0.2 * The include will now be inserted after `#pragma once` (if present) if there are no other include statements present ### 0.0.1 * First release - 一些调整过的部分简要说明 ```ts // 包围计算 // 不存在包围时,使用 <> // >say.h => // "say.h => "say.h" // => // "say.h" => "say.h" if (value[0] !== '<' && value[0] !== '\"' && value[0] !== '>') { value = `<${value}>`; } else if (value[0] === '>') { value = value.substring(1) value = `<${value}>`; } else if (value[0] === '"' && value[value.length -1] !== '"') { value = `${value}"`; } // 获取一个有效的编辑器,可能具有焦点,否则不进行处理 // 获取全文,并使用正则查找一个即将插入的位置 // /[ \t]* 任何可能有空格的的开始 // #\s* 接着是 # 与可能包含任何空格的部分 // () 抓取一个匹配点 // 第一种匹配 // include\s 这个点开头为 include 与单个任意空白 // +[<\"] 追加可能的部分 < 或 " // [^>\"]* 紧接着是 非 > 或 " 部分 // [>\"] 紧接着是 > 或 " 部分 // 第二种匹配 // \s* 任意空白 // pragma 紧接着是 pragma (所以这是一个关键点) // \s* 任意空白 // once 紧接着是 once (所以这是一个关键点) // \s* 任意空白 const editor = vscode.window.activeTextEditor; if (editor) { const text = editor.document.getText(); const regex = /[ \t]*#\s*(include\s+[<\"][^>\"]*[>\"]\s|\s*pragma\s*once\s)/g;g; var match; // 插入位置 // 一直接进行向下匹配,如果发现了没有了,这个点就是插入点 var insertOffset = 0; do { match = regex.exec(text); if (match) { // 找到结果的搜索的索引 // 在索引的起始位置,计算索引的长度并向后移一位 insertOffset = match.index + match[0].length + 1; } } while (match); editor.edit((editBuilder) => { editBuilder.insert( editor.document.positionAt(insertOffset), `#include ${value}\n`) }); } ```