From 83bc4afca416babf4c2554587f6eb0b795878559 Mon Sep 17 00:00:00 2001 From: Sergey Ignatov Date: Wed, 4 Jun 2014 17:37:44 +0400 Subject: [PATCH] move AddSpaceInsertHandler.java to lang-impl --- .../completion/AddSpaceInsertHandler.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java new file mode 100644 index 000000000000..d0e990654f3d --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/AddSpaceInsertHandler.java @@ -0,0 +1,36 @@ +package com.intellij.codeInsight.completion; + +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorModificationUtil; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDocumentManager; + +/** + * @author zolotov + */ +public class AddSpaceInsertHandler implements InsertHandler { + public final static InsertHandler INSTANCE = new AddSpaceInsertHandler(); + + public void handleInsert(InsertionContext context, LookupElement item) { + Editor editor = context.getEditor(); + if (context.getCompletionChar() == ' ') return; + Project project = editor.getProject(); + if (project != null) { + if (!isCharAtSpace(editor)) { + EditorModificationUtil.insertStringAtCaret(editor, " "); + PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); + } + else { + editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() + 1); + } + } + } + + private static boolean isCharAtSpace(Editor editor) { + final int startOffset = editor.getCaretModel().getOffset(); + final Document document = editor.getDocument(); + return document.getTextLength() > startOffset && document.getCharsSequence().charAt(startOffset) == ' '; + } +}