Skip to content
This repository has been archived by the owner on Dec 23, 2022. It is now read-only.

Add code format for import keyword completion #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testOptionAtTopLevel() {
public void testImportAtTopLevel() {
setInput("imp" + CARET_TAG);
assertTrue(completeWithUniqueChoice());
assertResult("import ");
assertResult("import \"\";");
}

public void testMessageInMessageAutoInsertSpace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.util.PsiTreeUtil;
Expand Down Expand Up @@ -232,6 +234,27 @@ protected void addCompletions(
}

private static LookupElement lookupElementWithSpace(String keyword) {
return LookupElementBuilder.create(keyword).withInsertHandler(AddSpaceInsertHandler.INSTANCE);
boolean needQuotas = "import".equalsIgnoreCase(keyword);
return LookupElementBuilder.create(keyword).withInsertHandler(
needQuotas ? new PbKeywordInsertHandler() : AddSpaceInsertHandler.INSTANCE);
}

private static class PbKeywordInsertHandler extends BasicInsertHandler<LookupElement> {

@Override
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement item) {
super.handleInsert(context, item);
Editor editor = context.getEditor();

Document document = editor.getDocument();
if (!document.getText().substring(context.getTailOffset()).startsWith(";")) {
document.insertString(context.getTailOffset(), ";");
}

int offset = editor.getCaretModel().getOffset();
document.insertString(offset, " \"");
document.insertString(offset + 2, "\"");
editor.getCaretModel().moveToOffset(offset + 2);
}
}
}