mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
move logic related to handling Enter in line comments from EnterHandler to EnterInLineCommentHandler
This commit is contained in:
+7
-14
@@ -113,12 +113,11 @@ public class CodeDocumentationUtil {
|
||||
@NotNull
|
||||
public static CommentContext tryParseCommentContext(@NotNull PsiFile file, @NotNull CharSequence chars, int offset, int lineStartOffset) {
|
||||
Commenter langCommenter = LanguageCommenters.INSTANCE.forLanguage(PsiUtilCore.getLanguageAtOffset(file, offset));
|
||||
return tryParseCommentContext(langCommenter, chars, offset, lineStartOffset);
|
||||
return tryParseCommentContext(langCommenter, chars, lineStartOffset);
|
||||
}
|
||||
|
||||
static CommentContext tryParseCommentContext(@Nullable Commenter langCommenter,
|
||||
@NotNull CharSequence chars,
|
||||
int offset,
|
||||
int lineStartOffset) {
|
||||
final boolean isInsideCommentLikeCode = langCommenter instanceof CodeDocumentationAwareCommenter;
|
||||
if (!isInsideCommentLikeCode) {
|
||||
@@ -133,11 +132,7 @@ public class CodeDocumentationUtil {
|
||||
&& CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getBlockCommentPrefix());
|
||||
boolean docAsterisk = commenter.getDocumentationCommentLinePrefix() != null
|
||||
&& CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getDocumentationCommentLinePrefix());
|
||||
final int firstNonSpaceInLine = CharArrayUtil.shiftForward(chars, offset, " \t");
|
||||
boolean slashSlash = commenter.getLineCommentPrefix() != null
|
||||
&& CharArrayUtil.regionMatches(chars, commentStartOffset, commenter.getLineCommentPrefix())
|
||||
&& firstNonSpaceInLine < chars.length() && chars.charAt(firstNonSpaceInLine) != '\n';
|
||||
return new CommentContext(commenter, docStart, cStyleStart, docAsterisk, slashSlash, commentStartOffset);
|
||||
return new CommentContext(commenter, docStart, cStyleStart, docAsterisk, commentStartOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,21 +152,19 @@ public class CodeDocumentationUtil {
|
||||
/** Indicates position at the line that starts from {@code '*'} (non-first and non-last javadoc line in java language). */
|
||||
public boolean docAsterisk;
|
||||
|
||||
/** Indicates position at the line that starts from {@code '//'} (in java language). */
|
||||
public boolean slashSlash;
|
||||
|
||||
public CommentContext() {
|
||||
commenter = null;
|
||||
lineStart = 0;
|
||||
}
|
||||
|
||||
public CommentContext(CodeDocumentationAwareCommenter commenter, boolean docStart, boolean cStyleStart, boolean docAsterisk,
|
||||
boolean slashSlash, int lineStart)
|
||||
{
|
||||
public CommentContext(CodeDocumentationAwareCommenter commenter,
|
||||
boolean docStart,
|
||||
boolean cStyleStart,
|
||||
boolean docAsterisk,
|
||||
int lineStart) {
|
||||
this.docStart = docStart;
|
||||
this.cStyleStart = cStyleStart;
|
||||
this.docAsterisk = docAsterisk;
|
||||
this.slashSlash = slashSlash;
|
||||
this.commenter = commenter;
|
||||
this.lineStart = lineStart;
|
||||
}
|
||||
|
||||
@@ -357,7 +357,7 @@ public class EnterHandler extends BaseEnterHandler {
|
||||
Language language = myDataContext instanceof UserDataHolder ? CONTEXT_LANGUAGE.get((UserDataHolder)myDataContext):null;
|
||||
Commenter langCommenter = language != null ? LanguageCommenters.INSTANCE.forLanguage(language) : null;
|
||||
CodeDocumentationUtil.CommentContext commentContext
|
||||
= CodeDocumentationUtil.tryParseCommentContext(langCommenter, chars, myOffset, lineStart);
|
||||
= CodeDocumentationUtil.tryParseCommentContext(langCommenter, chars, lineStart);
|
||||
|
||||
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(getProject());
|
||||
if (commentContext.docStart) {
|
||||
@@ -445,9 +445,7 @@ public class EnterHandler extends BaseEnterHandler {
|
||||
|
||||
boolean docIndentApplied = false;
|
||||
CodeInsightSettings codeInsightSettings = CodeInsightSettings.getInstance();
|
||||
if (codeInsightSettings.SMART_INDENT_ON_ENTER || myForceIndent || commentContext.docStart || commentContext.docAsterisk
|
||||
|| commentContext.slashSlash)
|
||||
{
|
||||
if (codeInsightSettings.SMART_INDENT_ON_ENTER || myForceIndent || commentContext.docStart || commentContext.docAsterisk) {
|
||||
final int offset = adjustLineIndentNoCommit(getLanguage(myDataContext), myDocument, myEditor, myOffset);
|
||||
if (offset >= 0) {
|
||||
myOffset = offset;
|
||||
@@ -467,7 +465,7 @@ public class EnterHandler extends BaseEnterHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if ((commentContext.docAsterisk || commentContext.docStart || commentContext.slashSlash) && !docIndentApplied) {
|
||||
if ((commentContext.docAsterisk || commentContext.docStart) && !docIndentApplied) {
|
||||
if (myInsertSpace) {
|
||||
if (myOffset == myDocument.getTextLength()) {
|
||||
myDocument.insertString(myOffset, " ");
|
||||
@@ -481,8 +479,8 @@ public class EnterHandler extends BaseEnterHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if ((commentContext.docAsterisk || commentContext.slashSlash) && !commentContext.docStart) {
|
||||
myCaretAdvance += commentContext.slashSlash ? commentContext.commenter.getLineCommentPrefix().trim().length() : 1;
|
||||
if (commentContext.docAsterisk && !commentContext.docStart) {
|
||||
myCaretAdvance += 1;
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
|
||||
+2
-1
@@ -46,7 +46,8 @@ public interface EnterHandlerDelegate {
|
||||
*
|
||||
* @param file The PSI file associated with the document.
|
||||
* @param editor The editor.
|
||||
* @param caretOffset A reference to the current caret offset in the document.
|
||||
* @param caretOffset Indicates a place where line break is to be inserted (it's a caret position initially). Method implementation
|
||||
* can change this value to adjust target line break position.
|
||||
* @param caretAdvance A reference to the number of columns by which the caret must be moved forward.
|
||||
* @param dataContext The data context passed to the enter handler.
|
||||
* @param originalHandler The original handler.
|
||||
|
||||
+21
-12
@@ -31,14 +31,19 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class EnterInLineCommentHandler extends EnterHandlerDelegateAdapter {
|
||||
private static final String WHITESPACE = " \t";
|
||||
|
||||
@Override
|
||||
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffsetRef, @NotNull final Ref<Integer> caretAdvance,
|
||||
@NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
|
||||
public Result preprocessEnter(@NotNull final PsiFile file,
|
||||
@NotNull final Editor editor,
|
||||
@NotNull final Ref<Integer> caretOffsetRef,
|
||||
@NotNull final Ref<Integer> caretAdvance,
|
||||
@NotNull final DataContext dataContext,
|
||||
final EditorActionHandler originalHandler) {
|
||||
final Language language = EnterHandler.getLanguage(dataContext);
|
||||
if (language == null) return Result.Continue;
|
||||
final Commenter languageCommenter = LanguageCommenters.INSTANCE.forLanguage(language);
|
||||
@@ -46,10 +51,11 @@ public class EnterInLineCommentHandler extends EnterHandlerDelegateAdapter {
|
||||
? (CodeDocumentationAwareCommenter)languageCommenter : null;
|
||||
if (commenter == null) return Result.Continue;
|
||||
int caretOffset = caretOffsetRef.get().intValue();
|
||||
if (isInLineComment(editor, caretOffset, commenter)) {
|
||||
int lineCommentStartOffset = getLineCommentStartOffset(editor, caretOffset, commenter);
|
||||
if (lineCommentStartOffset >= 0) {
|
||||
Document document = editor.getDocument();
|
||||
CharSequence text = document.getText();
|
||||
final int offset = CharArrayUtil.shiftForward(text, caretOffset, " \t");
|
||||
final int offset = CharArrayUtil.shiftForward(text, caretOffset, WHITESPACE);
|
||||
if (offset < document.getTextLength() && text.charAt(offset) != '\n') {
|
||||
String prefix = commenter.getLineCommentPrefix();
|
||||
assert prefix != null : "Line Comment type is set but Line Comment Prefix is null!";
|
||||
@@ -58,28 +64,31 @@ public class EnterInLineCommentHandler extends EnterHandlerDelegateAdapter {
|
||||
prefix += " ";
|
||||
}
|
||||
document.insertString(caretOffset, prefix);
|
||||
return Result.Default;
|
||||
}
|
||||
else {
|
||||
int afterPrefix = offset + prefix.length();
|
||||
if (afterPrefix < document.getTextLength() && text.charAt(afterPrefix) != ' ') {
|
||||
document.insertString(afterPrefix, " ");
|
||||
//caretAdvance.set(0);
|
||||
}
|
||||
caretOffsetRef.set(offset);
|
||||
}
|
||||
return Result.Default;
|
||||
|
||||
int beforeCommentOffset = CharArrayUtil.shiftBackward(text, lineCommentStartOffset - 1, WHITESPACE);
|
||||
if (beforeCommentOffset < 0 || text.charAt(beforeCommentOffset) == '\n') {
|
||||
caretAdvance.set(prefix.trim().length() + 1);
|
||||
}
|
||||
return Result.DefaultForceIndent;
|
||||
}
|
||||
}
|
||||
return Result.Continue;
|
||||
}
|
||||
|
||||
private static boolean isInLineComment(@NotNull Editor editor, int offset, @NotNull CodeDocumentationAwareCommenter commenter) {
|
||||
if (offset < 1) return false;
|
||||
private static int getLineCommentStartOffset(@NotNull Editor editor, int offset, @NotNull CodeDocumentationAwareCommenter commenter) {
|
||||
if (offset < 1) return -1;
|
||||
EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
|
||||
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
|
||||
String prefix = commenter.getLineCommentPrefix();
|
||||
return iterator.getTokenType() == commenter.getLineCommentTokenType()
|
||||
&& (iterator.getStart() + (prefix == null ? 0 : prefix.length())) <= offset;
|
||||
return iterator.getTokenType() == commenter.getLineCommentTokenType() &&
|
||||
(iterator.getStart() + (prefix == null ? 0 : prefix.length())) <= offset ? iterator.getStart() : -1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user