Skip to content

Commit

Permalink
Add #75 delimited comment syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kasecato committed Feb 23, 2019
1 parent 8e0e459 commit 5e97ea2
Show file tree
Hide file tree
Showing 12 changed files with 1,981 additions and 163 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 0.1.4 (Feb 23, 2019)

* enhancement - Upgrade libraries.
* enhancement - Support delimited comment syntax /** */? See [#75](https://github.com/kasecato/vscode-docomment/issues/75).

## 0.1.3 (Feb 1, 2019)

* enhancement - Upgrade libraries.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The menu under File > Preferences (Code > Preferences on Mac) provides entries t

```js
{
// Comments are single-line comments that start with three slashes (///) ("single"), or delimited comments that start with a slash and two stars (/**) ("delimited").
"docomment.syntax": "single",
// Press the Enter key to activate a command (Default: false)
"docomment.activateOnEnter": false,
// User-controllable options
Expand Down
1,928 changes: 1,818 additions & 110 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
"type": "object",
"title": "C# XML Documentation Comments configuration",
"properties": {
"docomment.syntax": {
"type": "string",
"enum": [
"single",
"delimited"
],
"default": "single",
"description": "Comments are single-line comments that start with three slashes (///) (\"single\"), or delimited comments that start with a slash and two stars (/**) (\"delimited\")."
},
"docomment.activateOnEnter": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -219,15 +228,15 @@
],
"dependencies": {},
"devDependencies": {
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.21",
"coveralls": "^3.0.2",
"@types/mocha": "^5.2.6",
"@types/node": "^11.9.5",
"coveralls": "^3.0.3",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"mocha": "^6.0.1",
"mocha-lcov-reporter": "^1.3.0",
"tslint": "^5.12.1",
"typescript": "^3.3.1",
"vscode": "^1.1.28"
"typescript": "^3.3.3333",
"vscode": "^1.1.30"
},
"extensionDependencies": [],
"scripts": {
Expand Down
9 changes: 5 additions & 4 deletions src/Controller/DocommentController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {window, workspace, Disposable, TextEditor, TextDocumentContentChangeEvent, WorkspaceConfiguration} from 'vscode';
import {IDocommentDomain} from '../Domain/IDocommentDomain';
import {IDocommentController} from './IDocommentController';
import {Configuration} from '../Entity/Config/Contributes/Configuration';
import { Disposable, TextDocumentContentChangeEvent, TextEditor, window, workspace, WorkspaceConfiguration } from 'vscode';
import { IDocommentDomain } from '../Domain/IDocommentDomain';
import { CommentSyntax, Configuration } from '../Entity/Config/Contributes/Configuration';
import { IDocommentController } from './IDocommentController';

export class DocommentController implements IDocommentController {

Expand Down Expand Up @@ -67,6 +67,7 @@ export class DocommentController implements IDocommentController {
const confEditor: WorkspaceConfiguration = workspace.getConfiguration(Configuration.KEY_EDITOR);

this._config = new Configuration();
this._config.syntax = CommentSyntax[confDocomment.get<string>(Configuration.SYNTAX, CommentSyntax.single)];
this._config.activateOnEnter = confDocomment.get<boolean>(Configuration.ACTIVATE_ON_ENTER, false);
this._config.advanced = confDocomment.get<Object>(Configuration.ADVANCED);
this._config.eol = confFiles.get<string>(Configuration.EOL, '\n');
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/DocommentDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export class DocommentDomain implements IDocommentDomain {

// Detect Code Type
const codeType: CodeType = this.GetCodeType(code);
console.log(codeType);
console.debug(codeType);
if (codeType === null) return;

// Gene Comment
const docomment = this.GeneDocomment(code, codeType);
console.log(docomment);
console.debug(docomment);
if (StringUtil.IsNullOrWhiteSpace(docomment)) return;

// Write Comment
Expand Down
35 changes: 14 additions & 21 deletions src/Domain/Lang/DocommentDomainCSharp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Position } from 'vscode';
import { Attribute, ConfigAdvancedCSharp } from '../../Entity/Config/Lang/ConfigAdvancedCSharp';
import { FormatterCSharp } from '../../Formatter/FormatterCSharp';
import { SyntacticAnalysisCSharp } from '../../SyntacticAnalysis/SyntacticAnalysisCSharp';
import { StringUtil } from '../../Utility/StringUtil';
import { DocommentDomain } from '../DocommentDomain';
import { CodeType } from '../IDocommentDomain';
import { Position } from 'vscode';
import { ConfigAdvancedCSharp, Attribute } from '../../Entity/Config/Lang/ConfigAdvancedCSharp';

export class DocommentDomainCSharp extends DocommentDomain {

Expand Down Expand Up @@ -33,36 +34,36 @@ export class DocommentDomainCSharp extends DocommentDomain {
}

// NG: KeyCode is NOT '/' or Enter
const isSlashKey: boolean = SyntacticAnalysisCSharp.IsSlashKey(activeChar);
const isActivationKey: boolean = SyntacticAnalysisCSharp.IsActivationKey(activeChar, this._config.syntax);
const isEnterKey: boolean = SyntacticAnalysisCSharp.IsEnterKey(activeChar, eventText);
if (!isSlashKey && !isEnterKey) {
if (!isActivationKey && !isEnterKey) {
return false;
}
this._isEnterKey = isEnterKey;

// NG: Activate on Enter NOT '/'
if (this._config.activateOnEnter) {
if (isSlashKey) {
if (isActivationKey) {
return false;
}
}

// NG: '////'
const activeLine: string = this._vsCodeApi.ReadLineAtCurrent();
if (isSlashKey) {
if (isActivationKey) {
// NG: '////'
if (!SyntacticAnalysisCSharp.IsDocCommentStrict(activeLine)) {
if (!SyntacticAnalysisCSharp.IsDocCommentStrict(activeLine, this._config.syntax)) {
return false;
}

// NG: '/' => Insert => Event => ' /// '
if (SyntacticAnalysisCSharp.IsDoubleDocComment(activeLine)) {
if (SyntacticAnalysisCSharp.IsDoubleDocComment(activeLine, this._config.syntax)) {
return false;
}
}
if (isEnterKey) {
// NG: '////'
if (!SyntacticAnalysisCSharp.IsDocComment(activeLine)) {
if (!SyntacticAnalysisCSharp.IsDocComment(activeLine, this._config.syntax)) {
return false;
}
}
Expand All @@ -84,7 +85,7 @@ export class DocommentDomainCSharp extends DocommentDomain {
// If the previous line was a doc comment and we hit enter.
// Extend the doc comment without generating anything else,
// even if there's a method or something next line.
if (!this._config.activateOnEnter && this._isEnterKey && SyntacticAnalysisCSharp.IsDocComment(this._vsCodeApi.ReadLineAtCurrent())) {
if (!this._config.activateOnEnter && this._isEnterKey && SyntacticAnalysisCSharp.IsDocComment(this._vsCodeApi.ReadLineAtCurrent(), this._config.syntax)) {
return CodeType.Comment;
}

Expand Down Expand Up @@ -163,7 +164,7 @@ export class DocommentDomainCSharp extends DocommentDomain {
hasValue = true;
break;
case CodeType.Comment:
return '/// ';
return SyntacticAnalysisCSharp.GetCommentSyntax(this._config.syntax) + ' ';
case CodeType.None:
return '';
default:
Expand Down Expand Up @@ -202,12 +203,11 @@ export class DocommentDomainCSharp extends DocommentDomain {
const indentBaseLine: string = this._vsCodeApi.ReadLineAtCurrent();
const indent: string = StringUtil.GetIndent(code, indentBaseLine, this._config.insertSpaces, this._config.detectIdentation);
const indentLen: number = StringUtil.GetIndentLen(indent, this._config.insertSpaces, this._config.detectIdentation);
const line = curPosition.line + 1;
const line = curPosition.line + SyntacticAnalysisCSharp.GetLineOffset(this._config.syntax, codeType);
const character = indentLen - 1 + docomment.length;
this._vsCodeApi.MoveSelection(line, character);
}


/*-------------------------------------------------------------------------
* Private Method
*-----------------------------------------------------------------------*/
Expand Down Expand Up @@ -258,14 +258,7 @@ export class DocommentDomainCSharp extends DocommentDomain {
// Format
const indentBaseLine: string = this._vsCodeApi.ReadLineAtCurrent();
const indent: string = StringUtil.GetIndent(code, indentBaseLine, this._config.insertSpaces, this._config.detectIdentation);
let docomment = ' ' + docommentList[0] + '\n';
for (let i = 1; i < docommentList.length; i++) {
docomment += indent + '/// ' + docommentList[i];
if (i !== docommentList.length - 1) {
docomment += '\n';
}
}

const docomment: string = FormatterCSharp.Format(docommentList, indent, this._config.syntax);
return docomment;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Entity/Config/Contributes/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class Configuration {
* docomment
*-----------------------------------------------------------------------*/
public static KEY_DOCOMMENT = 'docomment';
public static SYNTAX = 'syntax';
public static ACTIVATE_ON_ENTER = 'activateOnEnter';
public static ADVANCED = 'advanced';

Expand All @@ -20,10 +21,16 @@ export class Configuration {
public static INSERT_SPACES = 'insertSpaces';
public static DETECT_IDENTATION = 'detectIndentation';

public syntax: CommentSyntax;
public activateOnEnter: boolean;
public advanced: Object;
public eol: string;
public insertSpaces: boolean;
public detectIdentation: boolean;

}

export enum CommentSyntax {
single = 'single', // three slashes ///
delimited = 'delimited', // start with a slash and two stars /**
}
45 changes: 45 additions & 0 deletions src/Formatter/FormatterCSharp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { CommentSyntax } from "../Entity/Config/Contributes/Configuration";
import { SyntacticAnalysisCSharp } from "../SyntacticAnalysis/SyntacticAnalysisCSharp";

export class FormatterCSharp {

/*-------------------------------------------------------------------------
* Public Method: Formatter
*-----------------------------------------------------------------------*/
public static Format(docommentList: string[], indent: string, syntax: CommentSyntax) {
switch (syntax) {
case CommentSyntax.single:
return FormatterCSharp.FormatAsSingle(docommentList, indent, syntax);
case CommentSyntax.delimited:
return FormatterCSharp.FormatAsDelimited(docommentList, indent, syntax);
}
}

/*-------------------------------------------------------------------------
* Private Method: Formatter
*-----------------------------------------------------------------------*/
private static FormatAsSingle(docommentList: string[], indent: string, syntax: CommentSyntax) {
let docomment = ' ' + docommentList[0] + '\n';
for (let i = 1; i < docommentList.length; i++) {
docomment += indent + SyntacticAnalysisCSharp.GetCommentSyntax(syntax) + ' ' + docommentList[i];
if (i !== docommentList.length - 1) {
docomment += '\n';
}
}
return docomment;
}

private static FormatAsDelimited(docommentList: string[], indent: string, syntax: CommentSyntax) {
let docomment = '\n';
for (let i = 0; i < docommentList.length; i++) {
docomment += indent + ' ' + SyntacticAnalysisCSharp.GetCommentSyntax(syntax) + ' ' + docommentList[i];
if (i !== docommentList.length - 1) {
docomment += '\n';
}
}
docomment += '\n';
docomment += indent;
return docomment;
}

}
Loading

0 comments on commit 5e97ea2

Please sign in to comment.