use of org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings in project intellij-community by JetBrains.
the class GroovySpacingProcessorBasic method getSpacing.
public static Spacing getSpacing(GroovyBlock child1, GroovyBlock child2, FormattingContext context) {
ASTNode leftNode = child1.getNode();
ASTNode rightNode = child2.getNode();
final PsiElement left = leftNode.getPsi();
final PsiElement right = rightNode.getPsi();
IElementType leftType = leftNode.getElementType();
IElementType rightType = rightNode.getElementType();
final CommonCodeStyleSettings settings = context.getSettings();
final GroovyCodeStyleSettings groovySettings = context.getGroovySettings();
if (!(mirrorsAst(child1) && mirrorsAst(child2))) {
return NO_SPACING;
}
if (child2 instanceof ClosureBodyBlock) {
return settings.SPACE_WITHIN_BRACES ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
}
if (child1 instanceof ClosureBodyBlock) {
return createDependentSpacingForClosure(settings, groovySettings, (GrClosableBlock) left.getParent(), false);
}
if (leftType == GroovyDocElementTypes.GROOVY_DOC_COMMENT) {
return COMMON_SPACING_WITH_NL;
}
if (right instanceof GrTypeArgumentList) {
return NO_SPACING_WITH_NEWLINE;
}
/********** punctuation marks ************/
if (GroovyTokenTypes.mCOMMA == leftType) {
return settings.SPACE_AFTER_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
}
if (GroovyTokenTypes.mCOMMA == rightType) {
return settings.SPACE_BEFORE_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
}
if (GroovyTokenTypes.mSEMI == leftType) {
return settings.SPACE_AFTER_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
}
if (GroovyTokenTypes.mSEMI == rightType) {
return settings.SPACE_BEFORE_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
}
// For dots, commas etc.
if ((TokenSets.DOTS.contains(rightType)) || (GroovyTokenTypes.mCOLON.equals(rightType) && !(right.getParent() instanceof GrConditionalExpression))) {
return NO_SPACING_WITH_NEWLINE;
}
if (TokenSets.DOTS.contains(leftType)) {
return NO_SPACING_WITH_NEWLINE;
}
//todo:check it for multiple assignments
if ((GroovyElementTypes.VARIABLE_DEFINITION.equals(leftType) || GroovyElementTypes.VARIABLE_DEFINITION.equals(rightType)) && !(leftNode.getTreeNext() instanceof PsiErrorElement)) {
return Spacing.createSpacing(0, 0, 1, false, 100);
}
// For regexes
if (leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mREGEX_LITERAL || leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
return NO_SPACING;
}
// For << and >> ...
if ((GroovyTokenTypes.mLT.equals(leftType) && GroovyTokenTypes.mLT.equals(rightType)) || (GroovyTokenTypes.mGT.equals(leftType) && GroovyTokenTypes.mGT.equals(rightType))) {
return NO_SPACING_WITH_NEWLINE;
}
// Unary and postfix expressions
if (SpacingTokens.PREFIXES.contains(leftType) || SpacingTokens.POSTFIXES.contains(rightType) || (SpacingTokens.PREFIXES_OPTIONAL.contains(leftType) && left.getParent() instanceof GrUnaryExpression)) {
return NO_SPACING_WITH_NEWLINE;
}
if (SpacingTokens.RANGES.contains(leftType) || SpacingTokens.RANGES.contains(rightType)) {
return NO_SPACING_WITH_NEWLINE;
}
if (GroovyDocTokenTypes.mGDOC_ASTERISKS == leftType && GroovyDocTokenTypes.mGDOC_COMMENT_DATA == rightType) {
String text = rightNode.getText();
if (!text.isEmpty() && !StringUtil.startsWithChar(text, ' ')) {
return COMMON_SPACING;
}
return NO_SPACING;
}
if (leftType == GroovyDocTokenTypes.mGDOC_TAG_VALUE_TOKEN && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) {
return LAZY_SPACING;
}
if (left instanceof GrStatement && right instanceof GrStatement && left.getParent() instanceof GrStatementOwner && right.getParent() instanceof GrStatementOwner) {
return COMMON_SPACING_WITH_NL;
}
if (rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END) {
return NO_SPACING;
}
if ((leftType == GroovyDocElementTypes.GDOC_INLINED_TAG && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) || (leftType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA && rightType == GroovyDocElementTypes.GDOC_INLINED_TAG)) {
// Keep formatting between groovy doc text and groovy doc reference tag as is.
return NO_SPACING;
}
if (leftType == GroovyElementTypes.CLASS_TYPE_ELEMENT && rightType == GroovyTokenTypes.mTRIPLE_DOT) {
return NO_SPACING;
}
// diamonds
if (rightType == GroovyTokenTypes.mLT || rightType == GroovyTokenTypes.mGT) {
if (right.getParent() instanceof GrCodeReferenceElement) {
PsiElement p = right.getParent().getParent();
if (p instanceof GrNewExpression || p instanceof GrAnonymousClassDefinition) {
return NO_SPACING;
}
}
}
return COMMON_SPACING;
}
use of org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings in project intellij-community by JetBrains.
the class GroovyFormattingModelBuilder method createModel.
@Override
@NotNull
public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) {
ASTNode node = element.getNode();
assert node != null;
PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(GroovyLanguage.INSTANCE);
assert containingFile != null : element.getContainingFile();
ASTNode astNode = containingFile.getNode();
assert astNode != null;
CommonCodeStyleSettings groovySettings = settings.getCommonSettings(GroovyLanguage.INSTANCE);
GroovyCodeStyleSettings customSettings = settings.getCustomSettings(GroovyCodeStyleSettings.class);
final AlignmentProvider alignments = new AlignmentProvider();
if (customSettings.USE_FLYING_GEESE_BRACES) {
element.accept(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (GeeseUtil.isClosureRBrace(element)) {
GeeseUtil.calculateRBraceAlignment(element, alignments);
} else {
super.visitElement(element);
}
}
});
}
final GroovyBlock block = new GroovyBlock(astNode, Indent.getAbsoluteNoneIndent(), null, new FormattingContext(groovySettings, alignments, customSettings, false));
return new GroovyFormattingModel(containingFile, block, FormattingDocumentModelImpl.createOn(containingFile));
}
use of org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings in project intellij-community by JetBrains.
the class NamedArgumentInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
int tailOffset = context.getTailOffset();
PsiElement argumentList = context.getFile().findElementAt(tailOffset - 1);
while (argumentList != null && !(argumentList instanceof GrArgumentList) && !(argumentList instanceof GrListOrMap)) {
argumentList = argumentList.getParent();
}
final Editor editor = context.getEditor();
if (argumentList != null) {
CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(context.getProject()).getCurrentSettings();
GroovyCodeStyleSettings codeStyleSettings = settings.getCustomSettings(GroovyCodeStyleSettings.class);
CommonCodeStyleSettings commonCodeStyleSettings = settings.getCommonSettings(GroovyLanguage.INSTANCE);
boolean insertSpace = codeStyleSettings.SPACE_IN_NAMED_ARGUMENT;
if (context.getCompletionChar() == ':' || (insertSpace && context.getCompletionChar() == ' ')) {
context.setAddCompletionChar(false);
}
String argumentListText = argumentList.getText();
String s = argumentListText.substring(tailOffset - argumentList.getTextOffset());
s = StringUtil.trimEnd(s, ")");
if (s.trim().isEmpty()) {
String toInsert = insertSpace ? ": " : ":";
editor.getDocument().insertString(tailOffset, toInsert);
editor.getCaretModel().moveToOffset(tailOffset + toInsert.length());
} else {
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
char a = s.charAt(0);
if (Character.isLetterOrDigit(a)) {
return;
}
}
Matcher m = Pattern.compile("([ \\t]*):([ \\t]*)(.*)", Pattern.DOTALL).matcher(s);
if (m.matches()) {
int caret = tailOffset + m.end(2);
if (m.group(2).isEmpty()) {
editor.getDocument().insertString(caret, " ");
caret++;
}
editor.getCaretModel().moveToOffset(caret);
} else {
m = Pattern.compile("([ \\t]*)([\\n \\t]*)[\\],](.*)", Pattern.DOTALL).matcher(s);
if (m.matches()) {
String toInsert = insertSpace ? ": " : ":";
editor.getDocument().replaceString(tailOffset, tailOffset + m.start(2), toInsert);
editor.getCaretModel().moveToOffset(tailOffset + toInsert.length());
} else {
m = Pattern.compile("([ \\t]*)(.*)", Pattern.DOTALL).matcher(s);
if (!m.matches())
throw new RuntimeException("This pattern must match any non-empty string! (" + s + ")");
StringBuilder sb = new StringBuilder(3);
sb.append(':');
int shiftCaret = 1;
if (insertSpace) {
sb.append(' ');
shiftCaret++;
}
if (!m.group(2).startsWith("\n") && commonCodeStyleSettings.SPACE_AFTER_COMMA) {
sb.append(' ');
}
editor.getDocument().replaceString(tailOffset, tailOffset + m.start(2), sb);
editor.getCaretModel().moveToOffset(tailOffset + shiftCaret);
}
}
}
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
}
}
use of org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings in project intellij-community by JetBrains.
the class GroovyEnterHandler method handleFlyingGeese.
private static boolean handleFlyingGeese(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler, PsiFile file) {
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null)
return false;
GroovyCodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project).getCustomSettings(GroovyCodeStyleSettings.class);
if (!codeStyleSettings.USE_FLYING_GEESE_BRACES)
return false;
PsiElement element = file.findElementAt(caretOffset);
if (element != null && element.getNode().getElementType() == TokenType.WHITE_SPACE) {
element = GeeseUtil.getNextNonWhitespaceToken(element);
}
if (element == null || !GeeseUtil.isClosureRBrace(element))
return false;
element = GeeseUtil.getNextNonWhitespaceToken(element);
if (element == null || element.getNode().getElementType() != GroovyTokenTypes.mNLS || StringUtil.countChars(element.getText(), '\n') > 1) {
return false;
}
element = GeeseUtil.getNextNonWhitespaceToken(element);
if (element == null || !GeeseUtil.isClosureRBrace(element))
return false;
Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).commitDocument(document);
int toRemove = element.getTextRange().getStartOffset();
document.deleteString(caretOffset + 1, toRemove);
originalHandler.execute(editor, dataContext);
String text = document.getText();
int nextLineFeed = text.indexOf('\n', caretOffset + 1);
if (nextLineFeed == -1)
nextLineFeed = text.length();
CodeStyleManager.getInstance(project).reformatText(file, caretOffset, nextLineFeed);
return true;
}
Aggregations