Search in sources :

Example 1 with FormattingMode

use of com.intellij.formatting.FormattingMode in project intellij-community by JetBrains.

the class CodeStyleManagerRunnable method perform.

public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) {
    if (file instanceof PsiCompiledFile) {
        file = ((PsiCompiledFile) file).getDecompiledPsiFile();
    }
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject());
    Document document = documentManager.getDocument(file);
    if (document instanceof DocumentWindow) {
        final DocumentWindow documentWindow = (DocumentWindow) document;
        final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
        if (!file.equals(topLevelFile)) {
            if (range != null) {
                range = documentWindow.injectedToHost(range);
            }
            if (offset != -1) {
                offset = documentWindow.injectedToHost(offset);
            }
            return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow);
        }
    }
    final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file);
    if (templateFile != null) {
        file = templateFile;
        document = documentManager.getDocument(templateFile);
    }
    PsiElement element = null;
    if (offset != -1) {
        element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset);
        if (element == null && offset != file.getTextLength()) {
            return defaultValue;
        }
        if (isInsidePlainComment(offset, element)) {
            return computeValueInsidePlainComment(file, offset, defaultValue);
        }
    }
    final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
    FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder;
    if (builder != null && elementBuilder != null) {
        mySettings = CodeStyleSettingsManager.getSettings(myCodeStyleManager.getProject());
        mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
        myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);
        FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
        myCodeStyleManager.setCurrentFormattingMode(myMode);
        try {
            myModel = buildModel(builder, file, document);
            T result = doPerform(offset, range);
            if (result != null) {
                return result;
            }
        } finally {
            myCodeStyleManager.setCurrentFormattingMode(currentMode);
        }
    }
    return defaultValue;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) FormattingMode(com.intellij.formatting.FormattingMode) Document(com.intellij.openapi.editor.Document)

Aggregations

FormattingMode (com.intellij.formatting.FormattingMode)1 FormattingModelBuilder (com.intellij.formatting.FormattingModelBuilder)1 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 Document (com.intellij.openapi.editor.Document)1