This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from Shan1024/enter-handler
Add enter handler
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/org/ballerinalang/plugins/idea/formatter/BallerinaEnterBetweenBracesHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.ballerinalang.plugins.idea.formatter; | ||
|
||
import com.intellij.codeInsight.editorActions.enter.EnterBetweenBracesHandler; | ||
import com.intellij.openapi.actionSystem.DataContext; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.editor.EditorModificationUtil; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiDocumentManager; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiWhiteSpace; | ||
import org.ballerinalang.plugins.idea.BallerinaLanguage; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class BallerinaEnterBetweenBracesHandler extends EnterBetweenBracesHandler { | ||
|
||
@Override | ||
public Result postProcessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) { | ||
if (!file.getLanguage().is(BallerinaLanguage.INSTANCE)) { | ||
return Result.Continue; | ||
} | ||
if (isInsideABlock(file, editor)) { | ||
Project project = file.getProject(); | ||
// Todo - get the spacing from settings | ||
EditorModificationUtil.insertStringAtCaret(editor, " ", false, 4); | ||
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); | ||
} | ||
return Result.Continue; | ||
} | ||
|
||
private boolean isInsideABlock(PsiFile file, Editor editor) { | ||
// Get the offset of the caret | ||
int offset = editor.getCaretModel().getOffset(); | ||
// Get the element at the offset | ||
PsiElement element = file.findElementAt(offset); | ||
if (element == null) { | ||
return false; | ||
} | ||
|
||
// Check whether the previous non whitespace element is '{'. If so, that means the caret is within a block. | ||
int tempOffset = offset; | ||
PsiElement tempElement = element; | ||
while (tempElement instanceof PsiWhiteSpace && tempOffset > 0) { | ||
tempElement = file.findElementAt(--tempOffset); | ||
} | ||
if (tempElement != null && "{".equals(tempElement.getText())) { | ||
return true; | ||
} | ||
|
||
// Check whether the next non whitespace element is '}'. If so, that means the caret is within a block. | ||
tempOffset = offset; | ||
tempElement = element; | ||
while (tempElement instanceof PsiWhiteSpace && tempOffset < file.getTextLength()) { | ||
tempElement = file.findElementAt(++tempOffset); | ||
} | ||
if (tempElement != null && "}".equals(tempElement.getText())) { | ||
return true; | ||
} | ||
// Return false if the above conditions are not satisfied. | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean isBracePair(char c1, char c2) { | ||
return c1 == '{' && c2 == '}'; | ||
} | ||
} |