|
| 1 | +package org.nixos.idea.lang; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.editorActions.MultiCharQuoteHandler; |
| 4 | +import com.intellij.codeInsight.editorActions.QuoteHandler; |
| 5 | +import com.intellij.openapi.editor.Document; |
| 6 | +import com.intellij.openapi.editor.Editor; |
| 7 | +import com.intellij.openapi.editor.highlighter.HighlighterIterator; |
| 8 | +import com.intellij.psi.tree.IElementType; |
| 9 | +import com.intellij.psi.tree.TokenSet; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | +import org.nixos.idea.psi.NixTypes; |
| 13 | + |
| 14 | +/** |
| 15 | + * Quote handler for the Nix Language. |
| 16 | + * This class handles the automatic insertion of closing quotes after the user enters opening quotes. |
| 17 | + * The methods are called in the following order whenever the user enters a quote character. |
| 18 | + * <ol> |
| 19 | + * <li>{@link #isClosingQuote(HighlighterIterator, int)} is only called if the typed quote character already exists just behind the caret. |
| 20 | + * If the method returns {@code true}, the insertion of the quote and all further methods will be skipped. |
| 21 | + * The caret will just move over the existing quote one character to the right. |
| 22 | + * <li><em>*Insert quote character.*</em> |
| 23 | + * <li>Handling of {@link MultiCharQuoteHandler}:<ol> |
| 24 | + * <li>{@link #getClosingQuote(HighlighterIterator, int)} is called with the offset behind the inserted quote. |
| 25 | + * The returned value represents the string which shall be inserted. |
| 26 | + * Can return {@code null} to skipp further processing of {@code MultiCharQuoteHandler}. |
| 27 | + * <li>{@link #hasNonClosedLiteral(Editor, HighlighterIterator, int)} is called with the offset before the inserted quote. |
| 28 | + * The closing quotes returned by the previous method will only be inserted if this method returns {@code true}. |
| 29 | + * <li><em>*Insert closing quotes as returned by {@code getClosingQuote(...)}.*</em> |
| 30 | + * </ol> |
| 31 | + * <li>Standard handling of {@link QuoteHandler} (skipped if closing quotes were already inserted):<ol> |
| 32 | + * <li>{@link #isOpeningQuote(HighlighterIterator, int)} is called with the offset before the inserted quote. |
| 33 | + * The following steps will only be executed if this method returns {@code true}. |
| 34 | + * <li>{@link #hasNonClosedLiteral(Editor, HighlighterIterator, int)} is called with the offset before the inserted quote. |
| 35 | + * The following steps will only be executed if this method returns {@code true}. |
| 36 | + * <li><em>*Insert same quote character as initially typed by the user again.*</em> |
| 37 | + * </ol> |
| 38 | + * </ol> |
| 39 | + * |
| 40 | + * @see <a href="https://plugins.jetbrains.com/docs/intellij/additional-minor-features.html#quote-handling">Quote Handling Documentation</a> |
| 41 | + */ |
| 42 | +public final class NixQuoteHandler implements MultiCharQuoteHandler { |
| 43 | + |
| 44 | + private static final TokenSet CLOSING_QUOTE = TokenSet.create(NixTypes.STRING_CLOSE, NixTypes.IND_STRING_CLOSE); |
| 45 | + private static final TokenSet OPENING_QUOTE = TokenSet.create(NixTypes.STRING_OPEN, NixTypes.IND_STRING_OPEN); |
| 46 | + private static final TokenSet STRING_CONTENT = TokenSet.create(NixTypes.STR, NixTypes.STR_ESCAPE, NixTypes.IND_STR, NixTypes.IND_STR_ESCAPE); |
| 47 | + private static final TokenSet STRING_ANY = TokenSet.orSet(CLOSING_QUOTE, OPENING_QUOTE, STRING_CONTENT); |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean isClosingQuote(HighlighterIterator iterator, int offset) { |
| 51 | + return CLOSING_QUOTE.contains(iterator.getTokenType()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean isOpeningQuote(HighlighterIterator iterator, int offset) { |
| 56 | + // This method comes from QuoteHandler and assumes the quote is only one char in size. |
| 57 | + // We therefore ignore indented strings ('') in this method. |
| 58 | + // Note that this method is not actually used for the insertion of closing quotes, |
| 59 | + // as that is already handled by MultiCharQuoteHandler.getClosingQuote(...). See class documentation. |
| 60 | + // However, this method is also called by BackspaceHandler to delete the closing quotes of an empty string |
| 61 | + // when the opening quotes are removed. |
| 62 | + return NixTypes.STRING_OPEN == iterator.getTokenType(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean hasNonClosedLiteral(Editor editor, HighlighterIterator iterator, int offset) { |
| 67 | + IElementType openingToken = iterator.getTokenType(); |
| 68 | + if (iterator.getEnd() != offset + 1) { |
| 69 | + return false; // The caret isn't behind the opening quote. |
| 70 | + } else if (openingToken == NixTypes.STRING_OPEN) { |
| 71 | + // Insert closing quotes only if we would otherwise get a non-closed string at the end of the line. |
| 72 | + Document doc = editor.getDocument(); |
| 73 | + int lineEnd = doc.getLineEndOffset(doc.getLineNumber(offset)); |
| 74 | + while (true) { |
| 75 | + IElementType lastToken = iterator.getTokenType(); |
| 76 | + iterator.advance(); |
| 77 | + if (iterator.atEnd() || iterator.getStart() >= lineEnd) { |
| 78 | + return STRING_ANY.contains(lastToken) && !CLOSING_QUOTE.contains(lastToken); |
| 79 | + } |
| 80 | + } |
| 81 | + } else if (openingToken == NixTypes.IND_STRING_OPEN) { |
| 82 | + // Insert closing quotes only if we would otherwise get a non-closed string at the end of the file. |
| 83 | + while (true) { |
| 84 | + IElementType lastToken = iterator.getTokenType(); |
| 85 | + iterator.advance(); |
| 86 | + if (iterator.atEnd()) { |
| 87 | + return STRING_ANY.contains(lastToken) && !CLOSING_QUOTE.contains(lastToken); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean isInsideLiteral(HighlighterIterator iterator) { |
| 96 | + // Not sure why we need this. It seems to enable some special handling for escape sequences in IDEA. |
| 97 | + return STRING_ANY.contains(iterator.getTokenType()); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public @Nullable CharSequence getClosingQuote(@NotNull HighlighterIterator iterator, int offset) { |
| 102 | + // May need to retreat iterator by one token. |
| 103 | + // In contrast to all the other methods, this method is called with the offset behind the inserted quote. |
| 104 | + // However, the iterator may already be at the right location if the offset is at the end of the file. |
| 105 | + if (iterator.getEnd() != offset) { |
| 106 | + iterator.retreat(); |
| 107 | + if (iterator.atEnd()) { |
| 108 | + return null; // There was no previous token |
| 109 | + } |
| 110 | + } |
| 111 | + IElementType tokenType = iterator.getTokenType(); |
| 112 | + return tokenType == NixTypes.STRING_OPEN ? "\"" : |
| 113 | + tokenType == NixTypes.IND_STRING_OPEN ? "''" : |
| 114 | + null; |
| 115 | + } |
| 116 | +} |
0 commit comments