generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Implement DocumentationProvider #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/main/java/org/c3lang/intellij/annotation/DocComment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package org.c3lang.intellij.annotation | ||
|
|
||
| import ai.grazie.text.range | ||
| import com.intellij.lang.annotation.AnnotationHolder | ||
| import com.intellij.lang.annotation.HighlightSeverity | ||
| import com.intellij.openapi.editor.DefaultLanguageHighlighterColors | ||
| import com.intellij.openapi.util.TextRange | ||
| import com.intellij.psi.PsiComment | ||
| import com.intellij.psi.PsiWhiteSpace | ||
| import org.c3lang.intellij.psi.C3FuncDefinition | ||
|
|
||
| internal fun annotateDocComment(element: PsiComment, holder: AnnotationHolder) | ||
| { | ||
| annotateDocTags(element, holder) | ||
| annotateParamTags(element, holder) | ||
| } | ||
|
|
||
| private fun annotateDocTags(element: PsiComment, holder: AnnotationHolder) | ||
| { | ||
| val regex = Regex("@(param|return(!)?|deprecated|require|ensure|pure)") | ||
| val commentText = element.text | ||
| val commentStart = element.textRange.startOffset | ||
|
|
||
| regex.findAll(commentText).forEach { match -> | ||
| val range = TextRange(commentStart + match.range.first, commentStart + match.range.last + 1) | ||
|
|
||
| holder.newSilentAnnotation(HighlightSeverity.INFORMATION) | ||
| .range(range) | ||
| .textAttributes(DOC_COMMENT_TAG) | ||
| .create() | ||
| } | ||
| } | ||
|
|
||
| private fun getUnderlyingFunction(comment: PsiComment): C3FuncDefinition? | ||
| { | ||
| var next = comment.nextSibling | ||
|
|
||
| while (next is PsiWhiteSpace || next is PsiComment) | ||
| { | ||
| next = next.nextSibling | ||
| } | ||
|
|
||
| if (next.firstChild is C3FuncDefinition) return next.firstChild as C3FuncDefinition | ||
| return null | ||
| } | ||
|
|
||
| private fun annotateParamTags(element: PsiComment, holder: AnnotationHolder) | ||
| { | ||
| val function = getUnderlyingFunction(element) | ||
| val args = arrayListOf<String>() | ||
|
|
||
| if (function != null) | ||
| { | ||
| function.funcDef.fnParameterList.parameterList?.paramDeclList?.forEach { | ||
| args.add(it.parameter.name!!) | ||
| } | ||
| } | ||
|
|
||
| val regex = Regex("@param\\s+((\\[(in|&in|out|&out|inout|&inout)])\\s+)?(\\w+)(\\s+:\\s+(\"((?:[^\"\\\\]|\\\\.)*)\"|`((?:[^`\\\\]|\\\\.)*)`))?") | ||
| val commentText = element.text | ||
| val commentStart = element.textRange.startOffset | ||
|
|
||
| val nonMatchingLines = commentText.lines().filter { it.trim().startsWith("@param") && !regex.matches(it.trim()) } | ||
|
|
||
| nonMatchingLines.forEach { | ||
| holder.newAnnotation(HighlightSeverity.ERROR, "Invalid syntax") | ||
| .range(TextRange(commentStart + it.range.start, commentStart + it.range.endInclusive + 1)) | ||
| .create() | ||
| } | ||
|
|
||
| regex.findAll(commentText).forEach { match -> | ||
| val contract = match.groups[1] | ||
| val name = match.groups[4]!! | ||
| val description = match.groups[6] | ||
|
|
||
| if (!args.contains(name.value)) | ||
| { | ||
| val range = TextRange(commentStart + match.range.first, commentStart + match.range.last + 1) | ||
|
|
||
| holder.newAnnotation(HighlightSeverity.ERROR, "Argument missing in function") | ||
| .range(range) | ||
| .create() | ||
|
|
||
| return@forEach | ||
| } | ||
|
|
||
| if (contract != null) | ||
| { | ||
| val contractRange = TextRange(commentStart + contract.range.first, commentStart + contract.range.last + 1) | ||
|
|
||
| holder.newSilentAnnotation(HighlightSeverity.INFORMATION) | ||
| .range(contractRange) | ||
| .textAttributes(DefaultLanguageHighlighterColors.CONSTANT) | ||
| .create() | ||
| } | ||
|
|
||
| if (description != null) | ||
| { | ||
| val descriptionRange = TextRange(commentStart + description.range.first, commentStart + description.range.last + 1) | ||
|
|
||
| holder.newSilentAnnotation(HighlightSeverity.INFORMATION) | ||
| .range(descriptionRange) | ||
| .textAttributes(DefaultLanguageHighlighterColors.STRING) | ||
| .create() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package org.c3lang.intellij.annotation | ||
|
|
||
| import com.intellij.openapi.editor.colors.TextAttributesKey | ||
| import java.awt.Font | ||
|
|
||
| val DOC_COMMENT_TAG = TextAttributesKey.createTextAttributesKey("C3_DOC_COMMENT_TAG").apply { | ||
| defaultAttributes.fontType = Font.BOLD or Font.ITALIC // kotlin bitwise-or | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/c3lang/intellij/completion/DocCommentCompletionContributor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.c3lang.intellij.completion | ||
|
|
||
| import com.intellij.codeInsight.completion.CompletionParameters | ||
| import com.intellij.codeInsight.completion.CompletionProvider | ||
| import com.intellij.codeInsight.completion.CompletionResultSet | ||
| import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
| import com.intellij.patterns.PlatformPatterns.or | ||
| import com.intellij.patterns.PlatformPatterns.psiElement | ||
| import com.intellij.util.ProcessingContext | ||
| import org.c3lang.intellij.C3ParserDefinition | ||
|
|
||
| object DocCommentCompletionContributor : CompletionProvider<CompletionParameters>() | ||
| { | ||
| private val pattern = or( | ||
| psiElement().inside(psiElement().withElementType(C3ParserDefinition.DOC_COMMENT)), | ||
| ) | ||
|
|
||
| override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext, result: CompletionResultSet) | ||
| { | ||
| if (!pattern.accepts(parameters.position) && !pattern.accepts(parameters.originalPosition)) | ||
| { | ||
| return | ||
| } | ||
|
|
||
| listOf( | ||
| "@param", | ||
| "@return", | ||
| "@return!", | ||
| "@deprecated", | ||
| "@require", | ||
| "@ensure", | ||
| "@pure", | ||
| "[in]", | ||
| "[&in]", | ||
| "[out]", | ||
| "[&out]", | ||
| "[inout]", | ||
| "[&inout]" | ||
| ).forEach { | ||
| result.addElement(LookupElementBuilder.create(it)) | ||
| } | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/c3lang/intellij/docs/C3DocumentationProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.c3lang.intellij.docs | ||
|
|
||
| import com.intellij.lang.documentation.AbstractDocumentationProvider | ||
| import com.intellij.psi.PsiElement | ||
| import org.c3lang.intellij.psi.C3ConstDeclarationStmt | ||
| import org.c3lang.intellij.psi.C3FuncDef | ||
| import org.c3lang.intellij.psi.C3LocalDeclAfterType | ||
|
|
||
| class C3DocumentationProvider : AbstractDocumentationProvider() | ||
| { | ||
| override fun generateDoc(element: PsiElement?, originalElement: PsiElement?): String? | ||
| { | ||
| if (element is C3FuncDef) return generateFuncDefDoc(element) | ||
| if (element is C3LocalDeclAfterType) return generateVarDeclDoc(element) | ||
| if (element is C3ConstDeclarationStmt) return generateConstDeclDoc(element) | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| override fun generateHoverDoc(element: PsiElement, originalElement: PsiElement?): String? | ||
| { | ||
| return generateDoc(element, originalElement) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.c3lang.intellij.docs | ||
|
|
||
| import com.intellij.lang.documentation.DocumentationMarkup | ||
| import com.intellij.openapi.project.Project | ||
| import com.intellij.psi.presentation.java.SymbolPresentationUtil | ||
| import org.c3lang.intellij.psi.C3ConstDeclarationStmt | ||
|
|
||
| internal fun generateConstDeclDoc(element: C3ConstDeclarationStmt): String | ||
| { | ||
| val name = element.name ?: "Error getting name" | ||
| val type = element.type?.text ?: "Error getting type" | ||
| val value = element.expr?.text ?: "Error getting value" | ||
| val file = SymbolPresentationUtil.getFilePathPresentation(element.containingFile) | ||
|
|
||
| return renderFullDoc(file, name, value, type, element.project) | ||
| } | ||
|
|
||
| private fun renderFullDoc(file: String, name: String, value: String, type: String, project: Project): String | ||
| { | ||
| val builder = StringBuilder() | ||
| appendDefinition("const $type $name = $value", project, builder) | ||
| builder.append(DocumentationMarkup.SECTIONS_START) | ||
| appendFileSection(file, builder) | ||
| builder.append(DocumentationMarkup.SECTIONS_END) | ||
|
|
||
| return builder.toString() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not picking up the globals, could that be made to work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as of now i don't see a way to detect top level elements as it win't even recognize the module declaration or imports. the other changes are finished i just can't get around to getting this to work