# This is a combination of 2 commits.
# The first commit's message is: use more simple API # This is the 2nd commit message: More simple code
This commit is contained in:
committed by
Dmitry Jemerov
parent
7f5b1d489e
commit
be92ad2f2d
+1
-1
@@ -89,7 +89,7 @@ class KDocNameCompletionSession(parameters: CompletionParameters,
|
|||||||
|
|
||||||
private fun addLinkCompletions(declarationDescriptor: DeclarationDescriptor) {
|
private fun addLinkCompletions(declarationDescriptor: DeclarationDescriptor) {
|
||||||
val scope = getResolutionScope(resolutionFacade, declarationDescriptor)
|
val scope = getResolutionScope(resolutionFacade, declarationDescriptor)
|
||||||
scope.getDescriptors(nameFilter = {name -> prefixMatcher.prefixMatches(name.asString())}).forEach {
|
scope.getDescriptors(nameFilter = prefixMatcher.asNameFilter()).forEach {
|
||||||
val element = lookupElementFactory.createLookupElement(resolutionFacade, it, false)
|
val element = lookupElementFactory.createLookupElement(resolutionFacade, it, false)
|
||||||
resultSet.addElement(object: LookupElementDecorator<LookupElement>(element) {
|
resultSet.addElement(object: LookupElementDecorator<LookupElement>(element) {
|
||||||
override fun handleInsert(context: InsertionContext?) {
|
override fun handleInsert(context: InsertionContext?) {
|
||||||
|
|||||||
@@ -25,72 +25,75 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.psi.PsiDocumentManager
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens
|
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens
|
||||||
|
import org.jetbrains.kotlin.kdoc.psi.impl.KDocLink
|
||||||
import org.jetbrains.kotlin.lexer.JetTokens
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.JetFile
|
import org.jetbrains.kotlin.psi.JetFile
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
|
|
||||||
public class KDocTypedHandler(): TypedHandlerDelegate() {
|
public class KDocTypedHandler(): TypedHandlerDelegate() {
|
||||||
override fun beforeCharTyped(c: Char, project: Project, editor: Editor, file: PsiFile, fileType: FileType): TypedHandlerDelegate.Result =
|
override fun beforeCharTyped(c: Char, project: Project, editor: Editor, file: PsiFile, fileType: FileType): TypedHandlerDelegate.Result {
|
||||||
if (handleBeforeBracketTyped(c, editor, file)) TypedHandlerDelegate.Result.STOP else TypedHandlerDelegate.Result.CONTINUE
|
if (overwriteClosingBracket(c, editor, file)) {
|
||||||
|
EditorModificationUtil.moveCaretRelatively(editor, 1)
|
||||||
|
return TypedHandlerDelegate.Result.STOP
|
||||||
|
}
|
||||||
|
return TypedHandlerDelegate.Result.CONTINUE
|
||||||
|
}
|
||||||
|
|
||||||
override fun charTyped(c: Char, project: Project, editor: Editor, file: PsiFile): TypedHandlerDelegate.Result =
|
override fun charTyped(c: Char, project: Project, editor: Editor, file: PsiFile): TypedHandlerDelegate.Result =
|
||||||
if (handleBracketTyped(c, project, editor, file)) TypedHandlerDelegate.Result.STOP else TypedHandlerDelegate.Result.CONTINUE
|
if (handleBracketTyped(c, project, editor, file)) TypedHandlerDelegate.Result.STOP else TypedHandlerDelegate.Result.CONTINUE
|
||||||
|
|
||||||
private fun handleBeforeBracketTyped(c: Char, editor: Editor, file: PsiFile): Boolean {
|
private fun overwriteClosingBracket(c: Char, editor: Editor, file: PsiFile): Boolean {
|
||||||
if (file !is JetFile) {
|
if (c != ']' && c != ')') return false
|
||||||
return false
|
if (file !is JetFile) return false
|
||||||
}
|
if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) return false
|
||||||
if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val offset = editor.getCaretModel().getOffset()
|
val offset = editor.getCaretModel().getOffset()
|
||||||
val document = editor.getDocument()
|
val document = editor.getDocument()
|
||||||
if ((c == ']' || c == ')') && offset < document.getTextLength() && document.getCharsSequence().charAt(offset) == c) {
|
val chars = document.getCharsSequence()
|
||||||
|
if (offset < document.getTextLength() && chars[offset] == c) {
|
||||||
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document)
|
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document)
|
||||||
val element = file.findElementAt(offset)
|
|
||||||
// if the bracket is not part of a link, it will be part of KDOC_TEXT, not a separate RBRACKET element
|
val element = file.findElementAt(offset) ?: return false
|
||||||
if (c == ']' &&
|
val elementType = element.getNode().getElementType()
|
||||||
element?.getNode()?.getElementType() == JetTokens.RBRACKET ||
|
return when (c) {
|
||||||
(offset > 0 && document.getCharsSequence().charAt(offset - 1) == '[')) {
|
']' -> {
|
||||||
EditorModificationUtil.moveCaretRelatively(editor, 1)
|
// if the bracket is not part of a link, it will be part of KDOC_TEXT, not a separate RBRACKET element
|
||||||
return true
|
element.getParentOfType<KDocLink>(false) != null
|
||||||
}
|
&& (elementType == JetTokens.RBRACKET || (offset > 0 && chars[offset - 1] == '['))
|
||||||
if (c == ')' && element?.getNode()?.getElementType() == KDocTokens.MARKDOWN_INLINE_LINK) {
|
}
|
||||||
EditorModificationUtil.moveCaretRelatively(editor, 1)
|
|
||||||
return true
|
')' -> elementType == KDocTokens.MARKDOWN_INLINE_LINK
|
||||||
|
|
||||||
|
else -> false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleBracketTyped(c: Char, project: Project, editor: Editor, file: PsiFile): Boolean {
|
private fun handleBracketTyped(c: Char, project: Project, editor: Editor, file: PsiFile): Boolean {
|
||||||
if (file !is JetFile) {
|
if (c != '[' && c != '(') return false
|
||||||
return false
|
if (file !is JetFile) return false
|
||||||
}
|
if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) return false
|
||||||
if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
val offset = editor.getCaretModel().getOffset()
|
|
||||||
if (offset == 0) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == '[' || c == '(') {
|
val offset = editor.getCaretModel().getOffset()
|
||||||
val document = editor.getDocument()
|
if (offset == 0) return false
|
||||||
PsiDocumentManager.getInstance(project).commitDocument(document)
|
|
||||||
val element = file.findElementAt(offset - 1)
|
val document = editor.getDocument()
|
||||||
if (element == null ||
|
PsiDocumentManager.getInstance(project).commitDocument(document)
|
||||||
element.getNode().getElementType() != KDocTokens.TEXT) {
|
val element = file.findElementAt(offset - 1) ?: return false
|
||||||
return false
|
if (element.getNode().getElementType() != KDocTokens.TEXT) return false
|
||||||
}
|
|
||||||
if (c == '[')
|
when (c) {
|
||||||
{
|
'[' -> {
|
||||||
document.insertString(offset, "]")
|
document.insertString(offset, "]")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if (c == '(' && offset > 1 && document.getCharsSequence().charAt(offset - 2) == ']') {
|
|
||||||
document.insertString(offset, ")")
|
'(' -> {
|
||||||
return true
|
if (offset > 1 && document.getCharsSequence()[offset - 2] == ']') {
|
||||||
|
document.insertString(offset, ")")
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user