Skip to content

Commit

Permalink
Improve the autocompletion of Antlers delimiters
Browse files Browse the repository at this point in the history
If you type the opening {, the closing } will be added automatically. When you type the second opening {, the matching } will be added and the spaces will be inserted as well. This means, just type {{ will complete to {{ <caret> }}.
  • Loading branch information
Konafets committed Nov 23, 2022
1 parent 7babcff commit bbf01f0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
6 changes: 1 addition & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@

### Changed
- Use JVM toolchain for configuring source/target compilation compatibility
-

### Deprecated
- Change the completion of Antlers delimiters. If you type the opening `{`, the closing `}` will be added automatically. When you type the second opening `{`, the matching `}` will be added and the spaces will be inserted as well. This means, just type `{{` will complete to `{{ <caret> }}`.

### Removed

### Fixed

### Security

## [0.0.3] - 2022-08-08
### Added
- New `mount_url` tag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,71 @@
package de.arrobait.antlers.editor.actions;

import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
import com.intellij.lang.html.HTMLLanguage;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import de.arrobait.antlers.AntlersLanguage;
import de.arrobait.antlers.file.AntlersFileType;
import org.jetbrains.annotations.NotNull;

public class AntlersTypedHandler extends TypedHandlerDelegate {
@NotNull
@Override
public @NotNull Result beforeCharTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, @NotNull FileType fileType) {
public Result charTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
int offset = editor.getCaretModel().getOffset();
FileViewProvider provider = file.getViewProvider();

if (offset == 0 || offset > editor.getDocument().getTextLength()) {
if (!provider.getBaseLanguage().isKindOf(AntlersLanguage.INSTANCE)) {
return Result.CONTINUE;
}

String previousChar = editor.getDocument().getText(new TextRange(offset - 1, offset));
if (offset < 1 || offset > editor.getDocument().getTextLength()) {
return Result.CONTINUE;
}

if (file.getLanguage().equals(AntlersLanguage.INSTANCE)) {
PsiDocumentManager.getInstance(project).commitAllDocuments();
// TODO: Add the *.antlers.php extension here
if (file.getName().endsWith(AntlersFileType.DEFAULT_EXTENSION)) {
if (c == '{') {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());

// we suppress the built-in `}` auto-complete when we see `{{`
if (c == '{' && previousChar.equals("{")) {
// since the `}` autocomplete is built in to IDEA, we need to hack around it a bit by
// intercepting it before it is inserted, doing the work of inserting for the user
// by inserting the `{` the user just typed ...
editor.getDocument().insertString(offset, c + " }}");
// ... and position their caret after it as they would expect ...
editor.getCaretModel().moveToOffset(offset + 2);
if (offset < 2 || offset > editor.getDocument().getTextLength()) {
if (file.getLanguage().equals(HTMLLanguage.INSTANCE)) {
editor.getDocument().insertString(offset, "}");
editor.getCaretModel().moveToOffset(offset);
} else if (file.getLanguage().equals(AntlersLanguage.INSTANCE)) {
// TODO: That is the same like in the if branch.
editor.getDocument().insertString(offset, "}");
editor.getCaretModel().moveToOffset(offset);
}
} else {
String previousChars = editor.getDocument().getText(new TextRange(offset - 2, offset));
if (file.getLanguage().equals(HTMLLanguage.INSTANCE) && previousChars.equals("{{")) {
editor.getDocument().insertString(offset, " }");
editor.getCaretModel().moveToOffset(offset + 1);
} else if (file.getLanguage().equals(HTMLLanguage.INSTANCE)) {
editor.getDocument().insertString(offset, "}");
editor.getCaretModel().moveToOffset(offset);
} else if (file.getLanguage().equals(AntlersLanguage.INSTANCE)) {
// TODO: That is the same like in the else if branch.
editor.getDocument().insertString(offset, "}");
editor.getCaretModel().moveToOffset(offset);
}
}
}

if (c == '(') {
editor.getDocument().insertString(offset, ")");
editor.getCaretModel().moveToOffset(offset);
}

// ... then finally telling subsequent responses to this char typed to do nothing
return Result.STOP;
if (c == '[') {
editor.getDocument().insertString(offset, "]");
editor.getCaretModel().moveToOffset(offset);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AntlersBraceMatcher implements PairedBraceMatcher {

@Override
public boolean isPairedBracesAllowedBeforeType(@NotNull IElementType lbraceType, @Nullable IElementType contextType) {
return true;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

public class AntlersTypedHandlerTest extends AntlersActionHandlerTest {
public void testFirstCharTyped() {
doCharTest('{', "<caret>", "{<caret>");
doCharTest('{', "<caret>", "{<caret>}");
}

public void testSecondCharTyped() {
// When typing anything but {, do to auto-complete
doCharTest('x', "{<caret>", "{x<caret>");
doCharTest('x', "{<caret>}", "{x<caret>}");

doCharTest('{', "{<caret>", "{{ <caret> }}");
doCharTest('{', "{<caret>}", "{{ <caret> }}");
}

public void testParenthesis() {
Expand Down

0 comments on commit bbf01f0

Please sign in to comment.