From 25a212e923063ea61baf18127af546c4bd141c4e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 18 Jul 2014 21:58:11 +0400 Subject: [PATCH] Converted JetClassInsertHandler to Kotlin --- .../completion/DescriptorLookupConverter.java | 2 +- .../handlers/JetClassInsertHandler.java | 80 ------------------- .../handlers/JetClassInsertHandler.kt | 75 +++++++++++++++++ 3 files changed, 76 insertions(+), 81 deletions(-) delete mode 100644 idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.java create mode 100644 idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.kt diff --git a/idea/src/org/jetbrains/jet/plugin/completion/DescriptorLookupConverter.java b/idea/src/org/jetbrains/jet/plugin/completion/DescriptorLookupConverter.java index abf9c3813f6..f0a24f2a51f 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/DescriptorLookupConverter.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/DescriptorLookupConverter.java @@ -114,7 +114,7 @@ public final class DescriptorLookupConverter { } if (descriptor instanceof ClassDescriptor) { - return JetClassInsertHandler.INSTANCE; + return JetClassInsertHandler.INSTANCE$; } return null; diff --git a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.java b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.java deleted file mode 100644 index da741ff7142..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.plugin.completion.handlers; - -import com.intellij.codeInsight.completion.InsertHandler; -import com.intellij.codeInsight.completion.InsertionContext; -import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.RangeMarker; -import com.intellij.psi.PsiDocumentManager; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.lang.resolve.DescriptorUtils; -import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; -import org.jetbrains.jet.plugin.completion.JetLookupObject; - -public class JetClassInsertHandler implements InsertHandler { - - public static final InsertHandler INSTANCE = new JetClassInsertHandler(); - - @Override - public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement item) { - if (context.getFile() instanceof JetFile) { - if (item.getObject() instanceof JetLookupObject) { - JetLookupObject lookupObject = (JetLookupObject)item.getObject(); - DeclarationDescriptor descriptor = lookupObject.getDescriptor(); - if (descriptor != null) { - int startOffset = context.getStartOffset(); - Document document = context.getDocument(); - if (!isAfterDot(document, startOffset)) { - String fqName = DescriptorUtils.getFqName(descriptor).asString(); - // insert dot after because otherwise parser can sometimes produce no suitable reference here - String tempSuffix = ".xxx"; // we add "xxx" after dot because of some bugs in resolve (see KT-5145) - document.replaceString(startOffset, context.getTailOffset(), fqName + tempSuffix); - int classNameEnd = startOffset + fqName.length(); - - PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(context.getProject()); - psiDocumentManager.commitAllDocuments(); - RangeMarker rangeMarker = document.createRangeMarker(classNameEnd, classNameEnd + tempSuffix.length()); - - ShortenReferences.INSTANCE$.process((JetFile) context.getFile(), startOffset, classNameEnd); - psiDocumentManager.commitAllDocuments(); - psiDocumentManager.doPostponedOperationsAndUnblockDocument(document); - - if (rangeMarker.isValid()) { - document.deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset()); - } - } - } - } - } - } - - private static boolean isAfterDot(Document document, int offset) { - CharSequence chars = document.getCharsSequence(); - while(offset > 0) { - offset--; - char c = chars.charAt(offset); - if (!Character.isWhitespace(c)) { - return c == '.'; - } - } - return false; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.kt b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.kt new file mode 100644 index 00000000000..553e81cb053 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetClassInsertHandler.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.completion.handlers + +import com.intellij.codeInsight.completion.InsertHandler +import com.intellij.codeInsight.completion.InsertionContext +import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.openapi.editor.Document +import com.intellij.openapi.editor.RangeMarker +import com.intellij.psi.PsiDocumentManager +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor +import org.jetbrains.jet.lang.psi.JetFile +import org.jetbrains.jet.lang.resolve.DescriptorUtils +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences +import org.jetbrains.jet.plugin.completion.JetLookupObject + +public object JetClassInsertHandler : InsertHandler { + + override fun handleInsert(context: InsertionContext, item: LookupElement) { + val file = context.getFile() + if (file is JetFile) { + val descriptor = (item.getObject() as? JetLookupObject)?.getDescriptor() + if (descriptor != null) { + val startOffset = context.getStartOffset() + val document = context.getDocument() + if (!isAfterDot(document, startOffset)) { + val fqName = DescriptorUtils.getFqName(descriptor).asString() + // insert dot after because otherwise parser can sometimes produce no suitable reference here + val tempSuffix = ".xxx" // we add "xxx" after dot because of some bugs in resolve (see KT-5145) + document.replaceString(startOffset, context.getTailOffset(), fqName + tempSuffix) + val classNameEnd = startOffset + fqName.length() + + val psiDocumentManager = PsiDocumentManager.getInstance(context.getProject()) + psiDocumentManager.commitAllDocuments() + val rangeMarker = document.createRangeMarker(classNameEnd, classNameEnd + tempSuffix.length()) + + ShortenReferences.process(file, startOffset, classNameEnd) + psiDocumentManager.commitAllDocuments() + psiDocumentManager.doPostponedOperationsAndUnblockDocument(document) + + if (rangeMarker.isValid()) { + document.deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset()) + } + } + } + } + } + + private fun isAfterDot(document: Document, offset: Int): Boolean { + var curOffset = offset + val chars = document.getCharsSequence() + while (curOffset > 0) { + curOffset-- + val c = chars.charAt(curOffset) + if (!Character.isWhitespace(c)) { + return c == '.' + } + } + return false + } +}