Skip to content

Commit

Permalink
feat: Custom languages comments configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinhuish committed Dec 7, 2024
1 parent 8b83027 commit 1cc034b
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 27 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Forked from [aaron-bond/better-comments v3.0.2](https://github.com/aaron-bond/be
## Features

- [x] Fix matching errors.
- [x] Most languages that your editor correctly recognizes. See [#11](https://github.com/edwinhuish/better-comments-next/issues/11)
- [x] All languages supported.
- [x] Custom comments configuration for languages configurated by [`vscode.languages.setLanguageConfiguration`](https://code.visualstudio.com/api/references/vscode-api#languages)
- [x] Embedded languages supported. Like SFC of Vue, markdown, HTML, etc. See [#388](https://github.com/aaron-bond/better-comments/issues/388#issuecomment-1527426462)
- [x] Remote workspace supported. See [#507](https://github.com/aaron-bond/better-comments/issues/507)
- [x] Web editor supported.
- [x] Theme Switchable. Defferent tag config for light and dark themes. See [#506](https://github.com/aaron-bond/better-comments/issues/506)
- [x] Theme switchable. Defferent tag config for light and dark themes. See [#506](https://github.com/aaron-bond/better-comments/issues/506)
- [x] Allow multiple tags per item. See [#33](https://github.com/aaron-bond/better-comments/issues/33)
- [x] Multi-line comment supported. See [#7](https://github.com/edwinhuish/better-comments-next/issues/7)
- [ ] Custom comments for languages configurated by [`vscode.languages.setLanguageConfiguration`](https://code.visualstudio.com/api/references/vscode-api#languages)


## Decription
Expand All @@ -36,6 +36,8 @@ Default setting as below:
{
// Enable/disable hightlight plain text.
"better-comments.highlightPlainText": false,
// Custom languages comments configuration
"better-comments.languages": [],
// Overwrite the specified tag styles of `"better-comments.tags"` for light themes.
"better-comments.tagsLight": [],
// Overwrite the specified tag styles of `"better-comments.tags"` for dark themes.
Expand Down
49 changes: 48 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"publisher": "edwinhuish",
"name": "better-comments-next",
"displayName": "Better Comments Next",
"version": "3.3.7",
"version": "3.3.8",
"description": "Improve your code commenting by annotating with alert, informational, TODOs, and more!",
"author": {
"name": "Edwin Xu"
Expand Down Expand Up @@ -335,6 +335,53 @@
}
},
"default": []
},
"better-comments.languages": {
"type": "array",
"description": "List of languages that missing comments definition.",
"items": {
"type": "object",
"title": "language item",
"required": [
"id"
],
"properties": {
"id": {
"type": "string",
"description": "The language id"
},
"lineComment": {
"type": "string",
"description": "The line comment token, like `//`",
"examples": [
"//"
]
},
"blockComment": {
"type": "array",
"maxItems": 2,
"minItems": 2,
"description": "The block comment character pair, like `/*` `*/`",
"examples": [
"/*",
"*/"
]
},
"useDocComment": {
"type": "boolean",
"description": "Whether the language use doc comment",
"default": true
},
"embeddedLanguages": {
"type": "array",
"description": "List of embedded language ids",
"items": {
"type": "string",
"description": "The embedded language id"
}
}
}
}
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,39 @@ export interface TagFlatten extends Tag {
tagEscaped: string;
}

export interface Language {
/**
* The language id
*/
id: string;

/**
* The line comment token, like `//`
*/
lineComment: string;

/**
* The block comment character pair
*/
blockComment: vscode.CharacterPair;

/**
* Whether the language has doc comment
*/
useDocComment: boolean;

/**
* The embedded languages ids
*/
embeddedLanguages: string[];
}

interface Configuration {
useJSDocStyle: boolean;
highlightPlainText: boolean;
tags: Tag[];
tagsLight: Tag[];
tagsDark: Tag[];
languages: Language[];
}

export interface ConfigurationFlatten extends Configuration {
Expand Down Expand Up @@ -75,11 +102,11 @@ export function getConfigurationFlatten() {
const orig = getConfiguration();

configFlatten = {
useJSDocStyle: orig.useJSDocStyle,
highlightPlainText: orig.highlightPlainText,
tags: flattenTags(orig.tags),
tagsLight: flattenTags(orig.tagsLight),
tagsDark: flattenTags(orig.tagsDark),
languages: orig.languages,
};

return configFlatten;
Expand Down
21 changes: 19 additions & 2 deletions src/definition/definition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as langs from './langs';

import * as extConfig from '../configuration';

import * as vscode from 'vscode';

const cached = new Map<string, langs.Language>();
Expand Down Expand Up @@ -53,6 +55,21 @@ export function refresh() {
lang.setEmbeddedLanguages(embeddedLanguages);
}
}

const extConf = extConfig.getConfigurationFlatten();
for (const language of extConf.languages) {
const lang = useLanguage(language.id);

if (language.lineComment || language.blockComment.length) {
lang.setComments({ lineComment: language.lineComment, blockComment: language.blockComment });
}

if (language.embeddedLanguages) {
for (const embeddedLanguageCode of language.embeddedLanguages) {
lang.addEmbeddedLanguage(embeddedLanguageCode);
}
}
}
}

/**
Expand All @@ -76,11 +93,11 @@ export async function getAvailableComments(langId: string): Promise<langs.Availa

const comments = await lang.getComments();

if (comments.lineComment) {
if (comments?.lineComment) {
lineComments.add(comments.lineComment);
}

if (comments.blockComment) {
if (comments?.blockComment) {
const key = `${comments.blockComment[0]}${comments.blockComment[1]}`;
blockComments.set(key, comments.blockComment);
}
Expand Down
34 changes: 22 additions & 12 deletions src/definition/langs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ export class Language {
protected configurationUri?: vscode.Uri;
protected configuration?: vscode.LanguageConfiguration;

protected embeddedLanguages = new Set<string>();
protected availableComments: AvailableComments | undefined;
protected comments?: vscode.CommentRule;
protected embeddedLanguages: Set<string>;
protected availableComments?: AvailableComments;

constructor(id: string) {
this.id = id;

this.embeddedLanguages = new Set();
}

/**
Expand All @@ -37,10 +40,9 @@ export class Language {

/**
* Get language configuration
* @param forceRefresh force refresh configuration
*/
public async getConfiguration(forceRefresh = false) {
if (this.configuration && !forceRefresh) {
public async getConfiguration() {
if (this.configuration) {
return this.configuration;
}

Expand All @@ -64,18 +66,26 @@ export class Language {
}
}

public setComments(comments: vscode.CommentRule) {
this.comments = comments;
return this;
}

/**
* Get language comments rules
* @param forceRefresh force refresh configuration
*/
public async getComments(forceRefresh = false) {
const config = await this.getConfiguration(forceRefresh);

if (config && config.comments) {
return config.comments;
public async getComments(): Promise<vscode.CommentRule | undefined> {
if (!this.comments) {
const config = await this.getConfiguration();

if (config && config.comments) {
this.comments = config.comments;
} else {
this.comments = getDefaultComments(this.id);
}
}

return getDefaultComments(this.id) || {};
return this.comments;
}

/**
Expand Down
24 changes: 17 additions & 7 deletions src/handler/langs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,23 @@ export async function pickBlockCommentDecorationOptions({ editor, processed = []
return decorationOptions;
}

export async function pickDocCommentDecorationOptions({
editor,
processed = [],
marks = ['/**', '*/'],
}: PickDecorationOptionsParams & { marks?: [string, string] }) {
export async function pickDocCommentDecorationOptions({ editor, processed = [] }: PickDecorationOptionsParams) {
const decorationOptions = new Map<string, vscode.DecorationOptions[]>();

let marks: vscode.CharacterPair = ['/**', '*/'];

const config = configuration.getConfigurationFlatten();
const lang = config.languages.find((l) => l.id === editor.document.languageId);
if (lang) {
if (!lang.useDocComment) {
return decorationOptions;
}

if (lang.blockComment?.length) {
marks = lang.blockComment;
}
}

const start = escapeRegexString(marks[0]);
const end = escapeRegexString(marks[1]);
const prefix = escapeRegexString(marks[0].slice(-1));
Expand All @@ -306,8 +318,6 @@ export async function pickDocCommentDecorationOptions({
const lineTags = configs.tags.filter((t) => !t.multiline).map((tag) => tag.tagEscaped);
const lineExp = new RegExp(`(^|[ \\t]*(${prefix})[ \\t])(${lineTags.join('|')})([^\\n]*?)(\\n|$)`, 'ig');

const decorationOptions = new Map<string, vscode.DecorationOptions[]>();

const text = editor.document.getText();

let block: RegExpExecArray | null;
Expand Down

0 comments on commit 1cc034b

Please sign in to comment.