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' ; export class DocommentDomainCSharp extends DocommentDomain { private _isEnterKey : boolean = false ; private _isInsertDocCommentLineAbove : boolean = false ; public Init ( ) { this . _isEnterKey = false ; this . _isInsertDocCommentLineAbove = false ; } public IsTriggerDocomment ( ) { const eventText = this . _event ?. text ; if ( eventText == null || eventText === '' ) { return false ; } const activeChar = this . _vsCodeApi . ReadCharAtCurrent ( ) ; if ( activeChar == null ) { return false ; } const isAfterDocomment = SyntacticAnalysisCSharp . IsAfterDocomment ( eventText ) ; if ( isAfterDocomment ) { return false ; } const isActivationKey = SyntacticAnalysisCSharp . IsActivationKey ( activeChar , this . _config . syntax ) ; if ( isActivationKey ) { return this . IsTriggerDocommentByActivationKey ( ) ; } this . _isEnterKey = SyntacticAnalysisCSharp . IsEnterKey ( eventText ) ; if ( this . _isEnterKey ) { return this . IsTriggerDocommentByEnterKey ( eventText ) ; } return false ; } private IsTriggerDocommentByActivationKey ( ) { if ( this . _config . activateOnEnter ) { if ( ! this . _isEnterKey ) { return false ; } } const activeLine = this . _vsCodeApi . ReadLineAtCurrent ( ) ; if ( ! SyntacticAnalysisCSharp . IsDocCommentStrict ( activeLine , this . _config . syntax ) ) { return false ; } if ( SyntacticAnalysisCSharp . IsDoubleDocComment ( activeLine , this . _config . syntax ) ) { return false ; } return true ; } private IsTriggerDocommentByEnterKey ( eventText ) { const activeLine = this . _vsCodeApi . ReadLineAtCurrent ( ) ; if ( ! SyntacticAnalysisCSharp . IsDocComment ( activeLine , this . _config . syntax ) ) { return false ; } if ( SyntacticAnalysisCSharp . IsDocComment ( eventText , this . _config . syntax ) ) { return false ; } return true ; } public IsInScope ( ) { return true ; } public GetCode ( ) { const code = this . _vsCodeApi . ReadNextCodeFromCurrent ( this . _config . eol ) ; const removedAttr = code . split ( this . _config . eol ) . filter ( line => ! SyntacticAnalysisCSharp . IsAttribute ( line . trim ( ) ) ) . join ( '' ) ; return removedAttr ; } public GetCodeType ( code ) { if ( ! this . _config . activateOnEnter && this . _isEnterKey ) { if ( SyntacticAnalysisCSharp . IsDocComment ( this . _vsCodeApi . ReadLineAtCurrent ( ) , this . _config . syntax ) ) { return CodeType . Comment ; } } if ( SyntacticAnalysisCSharp . IsNamespace ( code ) ) return CodeType . Namespace ; if ( SyntacticAnalysisCSharp . IsClass ( code ) ) return CodeType . Class ; if ( SyntacticAnalysisCSharp . IsInterface ( code ) ) return CodeType . Interface ; if ( SyntacticAnalysisCSharp . IsStruct ( code ) ) return CodeType . Struct ; if ( SyntacticAnalysisCSharp . IsEnum ( code ) ) return CodeType . Enum ; if ( SyntacticAnalysisCSharp . IsDelegate ( code ) ) return CodeType . Delegate ; if ( SyntacticAnalysisCSharp . IsEvent ( code ) ) return CodeType . Event ; if ( SyntacticAnalysisCSharp . IsMethod ( code ) ) return CodeType . Method ; if ( SyntacticAnalysisCSharp . IsProperty ( code ) ) return CodeType . Property ; if ( SyntacticAnalysisCSharp . IsField ( code ) ) return CodeType . Field ; if ( SyntacticAnalysisCSharp . IsComment ( code ) ) return CodeType . Comment ; return CodeType . None ; } public GeneDocomment ( code , codeType ) { let genericList = null ; let paramNameList = null ; let hasReturn = false ; let hasValue = false ; switch ( codeType ) { case CodeType . Namespace : break ; case CodeType . Class : genericList = SyntacticAnalysisCSharp . GetGenericList ( code ) ; break ; case CodeType . Interface : genericList = SyntacticAnalysisCSharp . GetGenericList ( code ) ; break ; case CodeType . Struct : break ; case CodeType . Enum : break ; case CodeType . Delegate : genericList = SyntacticAnalysisCSharp . GetGenericMethodsList ( code ) ; paramNameList = SyntacticAnalysisCSharp . GetMethodParamNameList ( code ) ; hasReturn = SyntacticAnalysisCSharp . HasMethodReturn ( code ) ; break ; case CodeType . Event : break ; case CodeType . Method : genericList = SyntacticAnalysisCSharp . GetGenericMethodsList ( code ) ; paramNameList = SyntacticAnalysisCSharp . GetMethodParamNameList ( code ) ; hasReturn = SyntacticAnalysisCSharp . HasMethodReturn ( code ) ; break ; case CodeType . Field : break ; case CodeType . Property : hasValue = true ; break ; case CodeType . Comment : return SyntacticAnalysisCSharp . GetCommentSyntax ( this . _config . syntax ) + ' ' ; case CodeType . None : return '' ; } return this . GeneSummary ( code , codeType , genericList , paramNameList , hasReturn , hasValue ) ; } public WriteDocomment ( code , codeType , docomment ) { const position = this . _vsCodeApi . GetActivePosition ( ) ; if ( codeType === CodeType . Comment ) { const indentBaseLine = this . _vsCodeApi . ReadLineAtCurrent ( ) ; const indent = StringUtil . GetIndent ( code , indentBaseLine , this . _config . insertSpaces , this . _config . detectIdentation ) ; const indentLen = StringUtil . GetIndentLen ( indent , this . _config . syntax , this . _config . insertSpaces , this . _config . detectIdentation ) ; const lineOffset = this . _isInsertDocCommentLineAbove ? 0 : 1 ; const charOffset = this . _isInsertDocCommentLineAbove ? 0 : - 1 ; const insertPosition = this . _vsCodeApi . GetPosition ( position . line + lineOffset , indentLen + charOffset ) ; this . _vsCodeApi . InsertText ( insertPosition , docomment ) ; } else { if ( this . _isEnterKey ) { const active = this . _vsCodeApi . GetActivePosition ( ) ; const anchor = this . _vsCodeApi . GetPosition ( active . line + 1 , active . character ) ; const replaceSelection = this . _vsCodeApi . GetSelectionByPosition ( anchor , active ) ; this . _vsCodeApi . ReplaceText ( replaceSelection , docomment ) ; } else { const insertPosition = this . _vsCodeApi . ShiftPositionChar ( position , 1 ) ; this . _vsCodeApi . InsertText ( insertPosition , docomment ) ; } } } public MoveCursorTo ( code , codeType , docomment ) { const curPosition = this . _vsCodeApi . GetActivePosition ( ) ; const indentBaseLine = this . _vsCodeApi . ReadLineAtCurrent ( ) ; const indent = StringUtil . GetIndent ( code , indentBaseLine , this . _config . insertSpaces , this . _config . detectIdentation ) ; const indentLen = StringUtil . GetIndentLen ( indent , this . _config . syntax , 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 + lineOffset , character ) ; } private GeneSummary ( code , codeType , genericList , paramNameList , hasReturn , hasValue ) { let docommentList = new Array < string > ( ) ; if ( ConfigAdvancedCSharp . HasAttribute ( this . _config . advanced , codeType , Attribute . summary ) ) { docommentList . push ( '<summary>' ) ; docommentList . push ( '' ) ; docommentList . push ( '</summary>' ) ; } if ( ConfigAdvancedCSharp . HasAttribute ( this . _config . advanced , codeType , Attribute . param ) ) { if ( paramNameList !== null ) { paramNameList . forEach ( name => { docommentList . push ( '<param name="' + name + '"></param>' ) ; } ) ; } } if ( ConfigAdvancedCSharp . HasAttribute ( this . _config . advanced , codeType , Attribute . typeparam ) ) { if ( genericList !== null ) { genericList . forEach ( name => { docommentList . push ( '<typeparam name="' + name + '"></typeparam>' ) ; } ) ; } } if ( ConfigAdvancedCSharp . HasAttribute ( this . _config . advanced , codeType , Attribute . returns ) ) { if ( hasReturn ) { docommentList . push ( '<returns></returns>' ) ; } } if ( ConfigAdvancedCSharp . HasAttribute ( this . _config . advanced , codeType , Attribute . value ) ) { if ( hasValue ) { docommentList . push ( '<value></value>' ) ; } } const indentBaseLine = this . _vsCodeApi . ReadLineAtCurrent ( ) ; const indent = StringUtil . GetIndent ( code , indentBaseLine , this . _config . insertSpaces , this . _config . detectIdentation ) ; const docomment = FormatterCSharp . Format ( docommentList , indent , this . _config . syntax , this . _config . activateOnEnter ) ; return docomment ; } }