Skip to content

Commit 1dabaa8

Browse files
Add configurable quote character for indentation
Co-Authored-By: [email protected] <[email protected]>
1 parent 0768f5a commit 1dabaa8

File tree

9 files changed

+91
-19
lines changed

9 files changed

+91
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1616
### Added
1717
- Add `Summarize and New Context` command
1818
- Resolve `@import` directives in user selection text
19+
- Add configurable quote character for indentation (`markdown.copilot.indentation.quoteCharacter`)
1920

2021
### Changed
2122
- Make `@import` directives only activate at the beginning of a line
@@ -300,4 +301,4 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
300301
[0.0.4]: https://github.com/kurusugawa-computer/markdown-copilot-vscode/compare/v0.0.3...v0.0.4
301302
[0.0.3]: https://github.com/kurusugawa-computer/markdown-copilot-vscode/compare/v0.0.2...v0.0.3
302303
[0.0.2]: https://github.com/kurusugawa-computer/markdown-copilot-vscode/compare/v0.0.1...v0.0.2
303-
[0.0.1]: https://github.com/kurusugawa-computer/markdown-copilot-vscode/releases/tag/v0.0.1
304+
[0.0.1]: https://github.com/kurusugawa-computer/markdown-copilot-vscode/releases/tag/v0.0.1

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@
219219
"minimum": 0,
220220
"maximum": 1,
221221
"markdownDescription": "%config.options.temperature.description%"
222+
},
223+
"markdown.copilot.indentation.quoteCharacter": {
224+
"type": "string",
225+
"default": ">",
226+
"markdownDescription": "%config.indentation.quoteCharacter.description%"
222227
}
223228
}
224229
},

package.nls.ja.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
"config.backend.protocol.description": "バックエンドプロトコルを指定できます。`OpenAI`、`Azure`、`Ollama`、`OpenRouter`がサポートされています。",
3737
"config.options.model.description": "[Models](https://platform.openai.com/docs/models)にリストされているモデルを指定できます。`→ Model Name Text`を選択した場合、次の設定が使用されます。",
3838
"config.options.modelNameText.description": "バックエンドによって異なるモデル名をテキストとして指定できます。",
39-
"config.options.temperature.description": "`0.0`から`1.0`までの値、または空を指定できます。空の場合、この設定は省略されます。`0.8`のような高い値を設定すると出力はよりランダムになり、`0.2`のような低い値を設定すると出力はより集中して決定的になります。[FAQ](https://platform.openai.com/docs/guides/text-generation/how-should-i-set-the-temperature-parameter)が値の決定に役立ちます。"
39+
"config.options.temperature.description": "`0.0`から`1.0`までの値、または空を指定できます。空の場合、この設定は省略されます。`0.8`のような高い値を設定すると出力はよりランダムになり、`0.2`のような低い値を設定すると出力はより集中して決定的になります。[FAQ](https://platform.openai.com/docs/guides/text-generation/how-should-i-set-the-temperature-parameter)が値の決定に役立ちます。",
40+
"config.indentation.quoteCharacter.description": "Markdownの引用インデントに使用する文字を指定できます。デフォルトは `>` ですが、`>|` などの他の文字も使用できます。"
4041
}

package.nls.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
"config.backend.protocol.description": "Can specify the backend protocol. `OpenAI`, `Azure`, `Ollama`, `OpenRouter` are supported.",
3737
"config.options.model.description": "Can specify the models listed in [Models](https://platform.openai.com/docs/models), if `→ Model Name Text` selected, the following setting will be used.",
3838
"config.options.modelNameText.description": "Can specify the model name as text, which varies by backend.",
39-
"config.options.temperature.description": "Can specify a value between `0.0` and `1.0` or empty. If empty, the temperature setting is omitted. Higher values like `0.8` will make the output more random, while lower values like `0.2` will make it more focused and deterministic. [FAQ](https://platform.openai.com/docs/guides/text-generation/how-should-i-set-the-temperature-parameter) helps to determine the value."
40-
}
39+
"config.options.temperature.description": "Can specify a value between `0.0` and `1.0` or empty. If empty, the temperature setting is omitted. Higher values like `0.8` will make the output more random, while lower values like `0.2` will make it more focused and deterministic. [FAQ](https://platform.openai.com/docs/guides/text-generation/how-should-i-set-the-temperature-parameter) helps to determine the value.",
40+
"config.indentation.quoteCharacter.description": "Can specify the character(s) used for quote indentation in Markdown. Default is `>`, but you can use characters like `>|` or other combinations."
41+
}

package.nls.zh-cn.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
"config.backend.protocol.description": "可以指定后端协议。支持 `OpenAI`, `Azure`, `Ollama`, `OpenRouter`。",
3737
"config.options.model.description": "可指定[Models](https://platform.openai.com/docs/models)中列出的模型, 如果选择 `→ Model Name Text`, 将使用下一个配置。",
3838
"config.options.modelNameText.description": "可以文本形式指定模型名称, 不同后端可能有不同的命名规则。",
39-
"config.options.temperature.description": "可指定一个介于`0.0`和`1.0`之间的值或留空。如果留空, 则会省略该设置。`0.8`这样的较高值会使输出更加随机, 而`0.2`这样的较低值会使输出更加集中和确定。[FAQ](https://platform.openai.com/docs/guides/text-generation/how-should-i-set-the-temperature-parameter) 可帮助确定该值。"
39+
"config.options.temperature.description": "可指定一个介于`0.0`和`1.0`之间的值或留空。如果留空, 则会省略该设置。`0.8`这样的较高值会使输出更加随机, 而`0.2`这样的较低值会使输出更加集中和确定。[FAQ](https://platform.openai.com/docs/guides/text-generation/how-should-i-set-the-temperature-parameter) 可帮助确定该值。",
40+
"config.indentation.quoteCharacter.description": "可以指定用于 Markdown 引用缩进的字符。默认为 `>`,但您可以使用 `>|` 等其他字符组合。"
4041
}

src/extension.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { adjustStartToLineHead, toOverflowAdjustedRange } from './utils';
88
import * as config from './utils/configuration';
99
import { ContextDecorator, ContextOutline } from './utils/context';
1010
import { EditCursor } from './utils/editCursor';
11-
import { indentQuote, outdentQuote } from './utils/indention';
11+
import * as indention from './utils/indention';
1212
import * as l10n from './utils/localization';
1313
import * as logging from './utils/logging';
1414

@@ -73,7 +73,7 @@ export function activate(context: vscode.ExtensionContext) {
7373
textEditor.edit(editBuilder =>
7474
editBuilder.replace(
7575
range,
76-
indentQuote(textEditor.document.getText(range), 1)
76+
indention.indentQuote(textEditor.document.getText(range), 1)
7777
)
7878
);
7979
}));
@@ -86,7 +86,7 @@ export function activate(context: vscode.ExtensionContext) {
8686
textEditor.edit(editBuilder =>
8787
editBuilder.replace(
8888
range,
89-
outdentQuote(textEditor.document.getText(range), 1)
89+
indention.outdentQuote(textEditor.document.getText(range), 1)
9090
)
9191
);
9292
}));
@@ -118,6 +118,8 @@ export function activate(context: vscode.ExtensionContext) {
118118
catch { /* Ignore errors */ }
119119
try { contextDecorator.onDidChangeConfiguration(event); }
120120
catch { /* Ignore errors */ }
121+
try { indention.onDidChangeConfiguration(event); }
122+
catch { /* Ignore errors */ }
121123
}
122124
));
123125

@@ -192,4 +194,4 @@ export function activate(context: vscode.ExtensionContext) {
192194

193195
export function deactivate() {
194196
EditCursor.onDeactivate();
195-
}
197+
}

src/utils/configuration.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,9 @@ export class Configuration {
174174
get optionsTemperature(): number {
175175
return this.workspaceConfiguration.get<number>("markdown.copilot.options.temperature")!;
176176
}
177+
178+
get indentationQuoteCharacter(): string {
179+
const char = this.workspaceConfiguration.get<string>("markdown.copilot.indentation.quoteCharacter");
180+
return char !== undefined && char.trim().length > 0 ? char : ">";
181+
}
177182
}

src/utils/indention.ts

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,87 @@
11
/*
22
* Utilities for handling quote indentation in markdown text.
33
*/
4+
import * as vscode from 'vscode';
5+
import * as config from "./configuration";
6+
7+
let quoteIndentationPatternCache: RegExp | null = null;
8+
let quoteHeadPatternCache: RegExp | null = null;
9+
let currentQuoteChar: string = '';
10+
11+
/**
12+
* Initialize or update the RegExp cache based on the current configuration
13+
*/
14+
export function updateRegExpCache(): void {
15+
const quoteChar = config.get().indentationQuoteCharacter;
16+
17+
if (currentQuoteChar === quoteChar) { return; }
18+
currentQuoteChar = quoteChar;
19+
quoteIndentationPatternCache = new RegExp(`^(${quoteChar}[ \t]?)+`);
20+
quoteHeadPatternCache = new RegExp(`^(${quoteChar}[ \t]?)`);
21+
}
22+
23+
/**
24+
* Handle configuration changes for indentation settings
25+
*/
26+
export function onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent): void {
27+
if (!event.affectsConfiguration('markdown.copilot.indentation.quoteCharacter')) { return; }
28+
updateRegExpCache();
29+
}
30+
31+
/**
32+
* Get the cached quote indentation pattern
33+
*/
34+
function getQuoteIndentationPattern(): RegExp {
35+
if (!quoteIndentationPatternCache) {
36+
updateRegExpCache();
37+
}
38+
return quoteIndentationPatternCache!;
39+
}
40+
41+
/**
42+
* Get the cached quote head pattern
43+
*/
44+
function getQuoteHeadPattern(): RegExp {
45+
if (!quoteHeadPatternCache) {
46+
updateRegExpCache();
47+
}
48+
return quoteHeadPatternCache!;
49+
}
450

551
export function outdentQuote(text: string, level: number): string {
6-
return text.replace(new RegExp(`^(>[ \t]?){0,${level}}`, "gm"), "");
52+
const quoteChar = currentQuoteChar || config.get().indentationQuoteCharacter;
53+
return text.replace(new RegExp(`^(${quoteChar}[ \t]?){0,${level}}`, "gm"), "");
754
}
855

956
export function indentQuote(text: string, level: number): string {
10-
const headQuoteMatch = text.match(/^(>[ \t]?)/);
57+
const quoteChar = currentQuoteChar || config.get().indentationQuoteCharacter;
58+
const headQuoteMatch = text.match(getQuoteHeadPattern());
1159
const quoteIndentText = headQuoteMatch === null
12-
? "> "
60+
? quoteChar + " "
1361
: headQuoteMatch[1];
1462
return text.replace(/(?<!\r)^/gm, quoteIndentText.repeat(level));
1563
}
1664

17-
const quoteIndentationPattern = /^(>[ \t]?)+/;
1865
export function getQuoteIndent(lineText: string): string {
19-
const match = lineText.match(quoteIndentationPattern);
66+
const match = lineText.match(getQuoteIndentationPattern());
2067
if (match === null) { return ""; }
2168
return match[0];
2269
}
2370

2471
export function countQuoteIndent(lineText: string): number {
2572
const quoteHead = getQuoteIndent(lineText);
73+
const quoteChar = currentQuoteChar || config.get().indentationQuoteCharacter;
2674
let count = 0;
27-
for (const c of quoteHead) {
28-
count += +(c === '>');
75+
let i = 0;
76+
77+
while (i < quoteHead.length) {
78+
if (quoteHead.substring(i, i + quoteChar.length) === quoteChar) {
79+
count++;
80+
i += quoteChar.length;
81+
} else {
82+
i++;
83+
}
2984
}
85+
3086
return count;
31-
}
87+
}

0 commit comments

Comments
 (0)