Search in sources :

Example 11 with CodeStyleSettings

use of com.intellij.psi.codeStyle.CodeStyleSettings in project intellij-community by JetBrains.

the class PyWrapTest method testWrapRightMargin.

public void testWrapRightMargin() {
    final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings();
    final CommonCodeStyleSettings pythonSettings = settings.getCommonSettings(PythonLanguage.getInstance());
    int oldValue = pythonSettings.RIGHT_MARGIN;
    boolean oldMarginValue = settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN;
    pythonSettings.RIGHT_MARGIN = 100;
    settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = true;
    try {
        final String testName = "wrap/" + getTestName(true);
        myFixture.configureByFile(testName + ".py");
        for (int i = 0; i != 45; ++i) {
            myFixture.type(' ');
        }
        myFixture.checkResultByFile(testName + ".after.py");
    } finally {
        pythonSettings.RIGHT_MARGIN = oldValue;
        settings.WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN = oldMarginValue;
    }
}
Also used : CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings)

Example 12 with CodeStyleSettings

use of com.intellij.psi.codeStyle.CodeStyleSettings in project intellij-community by JetBrains.

the class GenerationNode method generate.

@NotNull
public TemplateImpl generate(@NotNull CustomTemplateCallback callback, @Nullable ZenCodingGenerator generator, @NotNull Collection<ZenCodingFilter> filters, boolean insertSurroundedText, int segmentsLimit) {
    myContainsSurroundedTextMarker = !(insertSurroundedText && myInsertSurroundedTextAtTheEnd);
    GenerationNode generationNode = this;
    if (generationNode != this) {
        return generationNode.generate(callback, generator, Collections.emptyList(), insertSurroundedText, segmentsLimit);
    }
    boolean shouldNotReformatTemplate = false;
    boolean oneLineTemplateExpanding = false;
    for (ZenCodingFilter filter : filters) {
        generationNode = filter.filterNode(generationNode);
        if (filter instanceof SingleLineEmmetFilter) {
            shouldNotReformatTemplate = true;
            oneLineTemplateExpanding = true;
        }
    }
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(callback.getProject());
    String indentStr;
    if (callback.isInInjectedFragment()) {
        Editor editor = callback.getEditor();
        Document document = editor.getDocument();
        if (document instanceof DocumentWindowImpl && ((DocumentWindowImpl) document).isOneLine()) {
            /* 
         * If document is one-line that in the moment of inserting text,
         * new line chars will be filtered (see DocumentWindowImpl#insertString).
         * So in this case we should filter text by SingleLineAvoid in order to avoid
         * inconsistency of template segments.
         */
            oneLineTemplateExpanding = true;
            filters.add(new SingleLineEmmetFilter());
        }
        indentStr = "";
    } else if (settings.useTabCharacter(callback.getFileType())) {
        indentStr = "\t";
    } else {
        int tabSize = settings.getTabSize(callback.getFileType());
        indentStr = StringUtil.repeatSymbol(' ', tabSize);
    }
    LiveTemplateBuilder builder = new LiveTemplateBuilder(EmmetOptions.getInstance().isAddEditPointAtTheEndOfTemplate(), segmentsLimit);
    int end = -1;
    boolean hasChildren = myChildren.size() > 0;
    TemplateImpl parentTemplate;
    Map<String, String> predefinedValues;
    if (generator instanceof XmlZenCodingGenerator) {
        TemplateToken xmlTemplateToken = myTemplateToken;
        parentTemplate = invokeXmlTemplate(xmlTemplateToken, callback, generator, hasChildren);
        predefinedValues = buildPredefinedValues(xmlTemplateToken.getAttributes(), (XmlZenCodingGenerator) generator, hasChildren);
    } else {
        parentTemplate = invokeTemplate(myTemplateToken, hasChildren, callback, generator);
        predefinedValues = null;
    }
    String s = parentTemplate.getString();
    for (ZenCodingFilter filter : filters) {
        s = filter.filterText(s, myTemplateToken);
    }
    parentTemplate = parentTemplate.copy();
    parentTemplate.setString(s);
    final String txt = hasChildren || myContainsSurroundedTextMarker ? null : mySurroundedText;
    parentTemplate = expandTemplate(parentTemplate, predefinedValues, txt, segmentsLimit);
    int offset = builder.insertTemplate(0, parentTemplate, null);
    int newOffset = gotoChild(callback.getProject(), builder.getText(), offset, 0, builder.length());
    if (offset < builder.length() && newOffset != offset) {
        end = offset;
    }
    offset = newOffset;
    if (end == -1 && offset < builder.length() && myChildren.size() == 0) {
        end = offset;
    }
    LiveTemplateBuilder.Marker marker = offset < builder.length() ? builder.createMarker(offset) : null;
    //noinspection ForLoopReplaceableByForEach
    for (int i = 0, myChildrenSize = myChildren.size(); i < myChildrenSize; i++) {
        GenerationNode child = myChildren.get(i);
        TemplateImpl childTemplate = child.generate(callback, generator, filters, !myContainsSurroundedTextMarker, segmentsLimit);
        boolean blockTag = child.isBlockTag();
        if (!oneLineTemplateExpanding && blockTag && !isNewLineBefore(builder.getText(), offset)) {
            builder.insertText(offset, "\n" + indentStr, false);
            offset += indentStr.length() + 1;
        }
        int e = builder.insertTemplate(offset, childTemplate, null);
        offset = marker != null ? marker.getEndOffset() : builder.length();
        if (!oneLineTemplateExpanding && ((blockTag && !isNewLineAfter(builder.getText(), offset)) || myInsertNewLineBetweenNodes)) {
            builder.insertText(offset, "\n" + indentStr, false);
            offset += indentStr.length() + 1;
        }
        if (end == -1 && e < offset) {
            end = e;
        }
    }
    if (shouldNotReformatTemplate) {
        builder.setIsToReformat(false);
    }
    return builder.buildTemplate();
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) SingleLineEmmetFilter(com.intellij.codeInsight.template.emmet.filters.SingleLineEmmetFilter) Document(com.intellij.openapi.editor.Document) ZenCodingFilter(com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) TemplateToken(com.intellij.codeInsight.template.emmet.tokens.TemplateToken) Editor(com.intellij.openapi.editor.Editor) LiveTemplateBuilder(com.intellij.codeInsight.template.LiveTemplateBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with CodeStyleSettings

use of com.intellij.psi.codeStyle.CodeStyleSettings in project intellij-community by JetBrains.

the class PyIndentUtil method getIndentSizeFromSettings.

/**
   * Returns indentation size configured in the Python code style settings.
   * 
   * @see #getIndentFromSettings(Project) 
   */
public static int getIndentSizeFromSettings(@NotNull Project project) {
    final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getInstance(project).getCurrentSettings();
    final CodeStyleSettings.IndentOptions indentOptions = codeStyleSettings.getIndentOptions(PythonFileType.INSTANCE);
    return indentOptions.INDENT_SIZE;
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings)

Example 14 with CodeStyleSettings

use of com.intellij.psi.codeStyle.CodeStyleSettings in project intellij-community by JetBrains.

the class GroovyCodeStyleManagerImpl method addLineFeedBefore.

protected void addLineFeedBefore(@NotNull PsiElement psiFile, @NotNull GrImportStatement result) {
    final CodeStyleSettings commonSettings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings();
    final GroovyCodeStyleSettings settings = commonSettings.getCustomSettings(GroovyCodeStyleSettings.class);
    final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
    final PackageEntry[] entries = layoutTable.getEntries();
    PsiElement prev = result.getPrevSibling();
    while (PsiImplUtil.isWhiteSpaceOrNls(prev)) {
        prev = prev.getPrevSibling();
    }
    if (PsiImplUtil.hasElementType(prev, GroovyTokenTypes.mSEMI))
        prev = prev.getPrevSibling();
    if (PsiImplUtil.isWhiteSpaceOrNls(prev))
        prev = prev.getPrevSibling();
    ASTNode node = psiFile.getNode();
    if (prev instanceof GrImportStatement) {
        final int idx_before = getPackageEntryIdx(entries, (GrImportStatement) prev);
        final int idx = getPackageEntryIdx(entries, result);
        final int spaceCount = getMaxSpaceCount(entries, idx_before, idx);
        //skip space and semicolon after import
        if (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling()) && PsiImplUtil.hasElementType(prev.getNextSibling().getNextSibling(), GroovyTokenTypes.mSEMI))
            prev = prev.getNextSibling().getNextSibling();
        while (PsiImplUtil.isWhiteSpaceOrNls(prev.getNextSibling())) {
            node.removeChild(prev.getNextSibling().getNode());
        }
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", spaceCount + 1), result.getNode());
    } else if (prev instanceof GrPackageDefinition) {
        node.addLeaf(GroovyTokenTypes.mNLS, StringUtil.repeat("\n", commonSettings.BLANK_LINES_AFTER_PACKAGE), result.getNode());
    }
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PackageEntry(com.intellij.psi.codeStyle.PackageEntry) PackageEntryTable(com.intellij.psi.codeStyle.PackageEntryTable) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) PsiElement(com.intellij.psi.PsiElement)

Example 15 with CodeStyleSettings

use of com.intellij.psi.codeStyle.CodeStyleSettings in project intellij-community by JetBrains.

the class FieldInitializerTailTypes method processTail.

@Override
public int processTail(Editor editor, int tailOffset) {
    CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(editor.getProject());
    Document document = editor.getDocument();
    CharSequence chars = document.getCharsSequence();
    int textLength = chars.length();
    if (tailOffset < textLength - 1 && chars.charAt(tailOffset) == ' ' && chars.charAt(tailOffset + 1) == '=') {
        return moveCaret(editor, tailOffset, 2);
    }
    if (tailOffset < textLength && chars.charAt(tailOffset) == '=') {
        return moveCaret(editor, tailOffset, 1);
    }
    if (styleSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS) {
        document.insertString(tailOffset, " =");
        tailOffset = moveCaret(editor, tailOffset, 2);
    } else {
        document.insertString(tailOffset, "=");
        tailOffset = moveCaret(editor, tailOffset, 1);
    }
    if (styleSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS) {
        tailOffset = insertChar(editor, tailOffset, ' ');
    }
    document.insertString(tailOffset, myText);
    return moveCaret(editor, tailOffset, myPosition);
}
Also used : CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) Document(com.intellij.openapi.editor.Document)

Aggregations

CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)194 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)85 Project (com.intellij.openapi.project.Project)13 XmlCodeStyleSettings (com.intellij.psi.formatter.xml.XmlCodeStyleSettings)12 Document (com.intellij.openapi.editor.Document)10 ECMA4CodeStyleSettings (com.intellij.lang.javascript.formatter.ECMA4CodeStyleSettings)9 JSCodeStyleSettings (com.intellij.lang.javascript.formatter.JSCodeStyleSettings)9 JavaCodeStyleSettings (com.intellij.psi.codeStyle.JavaCodeStyleSettings)9 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 PsiFile (com.intellij.psi.PsiFile)5 Nullable (org.jetbrains.annotations.Nullable)5 FormattingModel (com.intellij.formatting.FormattingModel)4 PsiElement (com.intellij.psi.PsiElement)4 CodeStyleSettingsManager (com.intellij.psi.codeStyle.CodeStyleSettingsManager)4 File (java.io.File)4 IOException (java.io.IOException)4 CodeStyleAbstractConfigurable (com.intellij.application.options.CodeStyleAbstractConfigurable)3 TabbedLanguageCodeStylePanel (com.intellij.application.options.TabbedLanguageCodeStylePanel)3 FormattingModelBuilder (com.intellij.formatting.FormattingModelBuilder)3