@@ -19,93 +19,93 @@ class CommentExtractor(private val fileExtractor: KotlinSourceFileExtractor) {
1919 private val logger = fileExtractor.logger
2020 private val ktFile = Psi2Ir ().getKtFile(file)
2121
22- init {
22+ fun extract () {
2323 if (ktFile == null ) {
2424 logger.warn(Severity .Warn , " Comments are not being processed in ${file.path} ." )
25+ } else {
26+ ktFile.accept(commentVisitor)
2527 }
2628 }
2729
28- fun extract () {
29- ktFile?.accept(
30- object : KtVisitor <Unit , Unit >() {
31- override fun visitElement (element : PsiElement ) {
32- element.acceptChildren(this )
30+ private val commentVisitor =
31+ object : KtVisitor <Unit , Unit >() {
32+ override fun visitElement (element : PsiElement ) {
33+ element.acceptChildren(this )
3334
34- // Slightly hacky, but `visitComment` doesn't seem to visit comments with `tokenType` `KtTokens.DOC_COMMENT`
35- if (element is PsiComment ){
36- visitCommentElement(element)
37- }
35+ // Slightly hacky, but `visitComment` doesn't seem to visit comments with `tokenType` `KtTokens.DOC_COMMENT`
36+ if (element is PsiComment ){
37+ visitCommentElement(element)
3838 }
39+ }
3940
40- private fun visitCommentElement (comment : PsiComment ) {
41- val type: CommentType = when (comment.tokenType) {
42- KtTokens .EOL_COMMENT -> {
43- CommentType .SingleLine
44- }
45- KtTokens .BLOCK_COMMENT -> {
46- CommentType .Block
47- }
48- KtTokens .DOC_COMMENT -> {
49- CommentType .Doc
50- }
51- else -> {
52- logger.warn(Severity .Warn , " Unhandled comment token type: ${comment.tokenType} " )
53- return
54- }
41+ private fun visitCommentElement (comment : PsiComment ) {
42+ val type: CommentType = when (comment.tokenType) {
43+ KtTokens .EOL_COMMENT -> {
44+ CommentType .SingleLine
5545 }
56-
57- val commentLabel = tw.getFreshIdLabel<DbKtcomment >()
58- tw.writeKtComments(commentLabel, type.value, escapeTrapString(comment.text))
59- val locId = tw.getLocation(comment.startOffset, comment.endOffset)
60- tw.writeHasLocation(commentLabel, locId)
61-
62- if (comment.tokenType != KtTokens .DOC_COMMENT ) {
63- return
46+ KtTokens .BLOCK_COMMENT -> {
47+ CommentType .Block
6448 }
65-
66- if (comment !is KDoc ) {
67- logger.warn(Severity .Warn , " Unexpected comment type with DocComment token type." )
68- return
49+ KtTokens .DOC_COMMENT -> {
50+ CommentType .Doc
6951 }
70-
71- for (sec in comment.getAllSections()) {
72- val commentSectionLabel = tw.getFreshIdLabel<DbKtcommentsection >()
73- tw.writeKtCommentSections(commentSectionLabel, commentLabel, escapeTrapString(sec.getContent()))
74- val name = sec.name
75- if (name != null ) {
76- tw.writeKtCommentSectionNames(commentSectionLabel, escapeTrapString(name))
77- }
78- val subjectName = sec.getSubjectName()
79- if (subjectName != null ) {
80- tw.writeKtCommentSectionSubjectNames(commentSectionLabel, escapeTrapString(subjectName))
81- }
52+ else -> {
53+ logger.warn(Severity .Warn , " Unhandled comment token type: ${comment.tokenType} " )
54+ return
8255 }
56+ }
8357
84- // Only storing the owner of doc comments:
85- val ownerPsi = getKDocOwner(comment) ? : return
58+ val commentLabel = tw.getFreshIdLabel<DbKtcomment >()
59+ tw.writeKtComments(commentLabel, type.value, escapeTrapString(comment.text))
60+ val locId = tw.getLocation(comment.startOffset, comment.endOffset)
61+ tw.writeHasLocation(commentLabel, locId)
8662
87- val owners = mutableListOf<IrElement >()
88- file.accept(IrVisitorLookup (ownerPsi, file), owners)
63+ if (comment.tokenType != KtTokens .DOC_COMMENT ) {
64+ return
65+ }
8966
90- for (ownerIr in owners) {
91- val label = fileExtractor.getLabel(ownerIr) ? : continue
92- val existingLabel = tw.getExistingLabelFor<DbTop >(label)
93- if (existingLabel == null ) {
94- logger.warn(Severity .Warn , " Couldn't get existing label for $label " )
95- continue
96- }
67+ if (comment !is KDoc ) {
68+ logger.warn(Severity .Warn , " Unexpected comment type with DocComment token type." )
69+ return
70+ }
9771
98- tw.writeKtCommentOwners(commentLabel, existingLabel)
72+ for (sec in comment.getAllSections()) {
73+ val commentSectionLabel = tw.getFreshIdLabel<DbKtcommentsection >()
74+ tw.writeKtCommentSections(commentSectionLabel, commentLabel, escapeTrapString(sec.getContent()))
75+ val name = sec.name
76+ if (name != null ) {
77+ tw.writeKtCommentSectionNames(commentSectionLabel, escapeTrapString(name))
78+ }
79+ val subjectName = sec.getSubjectName()
80+ if (subjectName != null ) {
81+ tw.writeKtCommentSectionSubjectNames(commentSectionLabel, escapeTrapString(subjectName))
9982 }
10083 }
10184
102- private fun getKDocOwner (comment : KDoc ) : PsiElement ? {
103- if (comment.owner == null ) {
104- logger.warn(Severity .Warn , " Couldn't get owner of KDoc." )
105- return null
85+ // Only storing the owner of doc comments:
86+ val ownerPsi = getKDocOwner(comment) ? : return
87+
88+ val owners = mutableListOf<IrElement >()
89+ file.accept(IrVisitorLookup (ownerPsi, file), owners)
90+
91+ for (ownerIr in owners) {
92+ val label = fileExtractor.getLabel(ownerIr) ? : continue
93+ val existingLabel = tw.getExistingLabelFor<DbTop >(label)
94+ if (existingLabel == null ) {
95+ logger.warn(Severity .Warn , " Couldn't get existing label for $label " )
96+ continue
10697 }
107- return comment.owner!!
98+
99+ tw.writeKtCommentOwners(commentLabel, existingLabel)
108100 }
109- })
110- }
101+ }
102+
103+ private fun getKDocOwner (comment : KDoc ) : PsiElement ? {
104+ val owner = comment.owner
105+ if (owner == null ) {
106+ logger.warn(Severity .Warn , " Couldn't get owner of KDoc." )
107+ }
108+ return owner
109+ }
110+ }
111111}
0 commit comments