From d4547e3fe839ca110196e8ca9c9819398fd541a7 Mon Sep 17 00:00:00 2001 From: kasecato Date: Sun, 26 Jul 2020 14:30:21 +0900 Subject: [PATCH] Fixed #98 ctrl-enter (insert line below, insert line above) in middle of line not adding --- CHANGELOG.md | 1 + src/Domain/DocommentDomain.ts | 8 +++++ src/Domain/IDocommentDomain.ts | 1 + src/Domain/Lang/DocommentDomainCSharp.ts | 30 +++++++++++++++---- .../SyntacticAnalysisCSharp.ts | 4 +++ 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a59997d..ee64f8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.1.15 (July 26, 2020) +* bug fix - ctrl-enter (insert line below, insert line above) in middle of line not adding `///`. See [#98](https://github.com/kasecato/vscode-docomment/issues/98). * bug fix - Delimited comment "/**" doen't work. See [#100](https://github.com/kasecato/vscode-docomment/issues/100). ## 0.1.14 (July 20, 2020) diff --git a/src/Domain/DocommentDomain.ts b/src/Domain/DocommentDomain.ts index f50abb3..158e8f0 100644 --- a/src/Domain/DocommentDomain.ts +++ b/src/Domain/DocommentDomain.ts @@ -40,6 +40,9 @@ export class DocommentDomain implements IDocommentDomain { // Detect Language if (!this._vsCodeApi.IsLanguage(languageId)) return; + // Initalize + this.Init(); + // Can Fire Document Comment if (!this.IsTriggerDocomment()) return; @@ -69,6 +72,11 @@ export class DocommentDomain implements IDocommentDomain { * Domain Method *-----------------------------------------------------------------------*/ + /* @implements */ + public Init() { + // NOP + } + /* @implements */ public IsTriggerDocomment(): boolean { return false; diff --git a/src/Domain/IDocommentDomain.ts b/src/Domain/IDocommentDomain.ts index 01d0f7f..4ab0ae6 100644 --- a/src/Domain/IDocommentDomain.ts +++ b/src/Domain/IDocommentDomain.ts @@ -42,6 +42,7 @@ export interface IDocommentDomain { /*------------------------------------------------------------------------- * Domain Method *-----------------------------------------------------------------------*/ + Init(); IsTriggerDocomment(): boolean; GetCode(): string; GetCodeType(code: string): CodeType; diff --git a/src/Domain/Lang/DocommentDomainCSharp.ts b/src/Domain/Lang/DocommentDomainCSharp.ts index 0422ceb..2234de9 100644 --- a/src/Domain/Lang/DocommentDomainCSharp.ts +++ b/src/Domain/Lang/DocommentDomainCSharp.ts @@ -12,11 +12,17 @@ export class DocommentDomainCSharp extends DocommentDomain { * Field *-----------------------------------------------------------------------*/ private _isEnterKey: boolean = false; + private _isInsertDocCommentLineAbove: boolean = false; /*------------------------------------------------------------------------- * Domain Method *-----------------------------------------------------------------------*/ + /* @override */ + public Init() { + this._isEnterKey = false; + this._isInsertDocCommentLineAbove = false; + } /* @override */ public IsTriggerDocomment(): boolean { @@ -70,12 +76,23 @@ export class DocommentDomainCSharp extends DocommentDomain { return false; } } + // NG: '////' - else if (!SyntacticAnalysisCSharp.IsDocComment(activeLine, this._config.syntax)) { - return false; + const isInsertLineAbove = SyntacticAnalysisCSharp.IsInsertLineAbove(activeLine); + if (isInsertLineAbove) { + const nextLine = this._vsCodeApi.ReadNextLineFromCurrent(); + const isInsertDocCommentLineAbove = SyntacticAnalysisCSharp.IsDocComment(nextLine, this._config.syntax); + if (!isInsertDocCommentLineAbove) { + return false; + } + this._isInsertDocCommentLineAbove = isInsertDocCommentLineAbove; + } else { + if (!SyntacticAnalysisCSharp.IsDocComment(activeLine, this._config.syntax)) { + return false; + } } // NG: Undo comment lines with the enter key - else if (SyntacticAnalysisCSharp.IsDocComment(eventText, this._config.syntax)) { + if (SyntacticAnalysisCSharp.IsDocComment(eventText, this._config.syntax)) { return false; } } @@ -194,7 +211,9 @@ 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 insertPosition: Position = this._vsCodeApi.GetPosition(position.line + 1, indentLen - 1); + const lineOffset = this._isInsertDocCommentLineAbove ? 0 : 1; + const charOffset = this._isInsertDocCommentLineAbove ? 0 : -1; + const insertPosition: Position = this._vsCodeApi.GetPosition(position.line + lineOffset, indentLen + charOffset); this._vsCodeApi.InsertText(insertPosition, docomment); } else { if (this._isEnterKey) { @@ -216,8 +235,9 @@ export class DocommentDomainCSharp extends DocommentDomain { 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 + SyntacticAnalysisCSharp.GetLineOffset(this._config.syntax, codeType); + const lineOffset = this._isInsertDocCommentLineAbove ? -1 : 0; const character = indentLen - 1 + docomment.length; - this._vsCodeApi.MoveSelection(line, character); + this._vsCodeApi.MoveSelection(line + lineOffset, character); } /*------------------------------------------------------------------------- diff --git a/src/SyntacticAnalysis/SyntacticAnalysisCSharp.ts b/src/SyntacticAnalysis/SyntacticAnalysisCSharp.ts index 9935fc4..8741958 100644 --- a/src/SyntacticAnalysis/SyntacticAnalysisCSharp.ts +++ b/src/SyntacticAnalysis/SyntacticAnalysisCSharp.ts @@ -16,6 +16,10 @@ export class SyntacticAnalysisCSharp { return (activeChar === '') && (text.startsWith('\n') || text.startsWith("\r\n")); } + public static IsInsertLineAbove(activeLine: string): boolean { + return activeLine !== null && activeLine.trim().length === 0 ; + } + public static IsActivationKey(activeChar: string, syntax: CommentSyntax): boolean { switch (syntax) { case CommentSyntax.single: