Search in sources :

Example 1 with LangCodeStyleExtractor

use of com.intellij.psi.codeStyle.extractor.differ.LangCodeStyleExtractor in project intellij-community by JetBrains.

the class ExtractCodeStyleAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return;
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    PsiFile file = null;
    if (editor == null && files != null && files.length == 1 && !files[0].isDirectory()) {
        file = PsiManager.getInstance(project).findFile(files[0]);
    } else if (editor != null) {
        file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    }
    if (file == null)
        return;
    Language language = file.getLanguage();
    final LangCodeStyleExtractor extractor = LangCodeStyleExtractor.EXTENSION.forLanguage(language);
    if (extractor == null)
        return;
    final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
    final CodeStyleDeriveProcessor genProcessor = new GenProcessor(extractor);
    final PsiFile finalFile = file;
    final Task.Backgroundable task = new Task.Backgroundable(project, "Code style extractor", true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                CodeStyleSettings cloneSettings = settings.clone();
                Map<Value, Object> backup = genProcessor.backupValues(cloneSettings, finalFile.getLanguage());
                ValuesExtractionResult res = genProcessor.runWithProgress(project, cloneSettings, finalFile, indicator);
                reportResult(res, project, cloneSettings, finalFile, backup);
            } catch (ProcessCanceledException e) {
                Utils.logError("Code extraction was canceled");
            } catch (Throwable t) {
                Utils.logError("Unexpected exception:\n" + t);
            }
        }
    };
    ProgressManager.getInstance().run(task);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) ValuesExtractionResult(com.intellij.psi.codeStyle.extractor.values.ValuesExtractionResult) CodeStyleDeriveProcessor(com.intellij.psi.codeStyle.extractor.processor.CodeStyleDeriveProcessor) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Value(com.intellij.psi.codeStyle.extractor.values.Value) PsiFile(com.intellij.psi.PsiFile) LangCodeStyleExtractor(com.intellij.psi.codeStyle.extractor.differ.LangCodeStyleExtractor) Editor(com.intellij.openapi.editor.Editor) GenProcessor(com.intellij.psi.codeStyle.extractor.processor.GenProcessor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

Language (com.intellij.lang.Language)1 Editor (com.intellij.openapi.editor.Editor)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 LangCodeStyleExtractor (com.intellij.psi.codeStyle.extractor.differ.LangCodeStyleExtractor)1 CodeStyleDeriveProcessor (com.intellij.psi.codeStyle.extractor.processor.CodeStyleDeriveProcessor)1 GenProcessor (com.intellij.psi.codeStyle.extractor.processor.GenProcessor)1 Value (com.intellij.psi.codeStyle.extractor.values.Value)1 ValuesExtractionResult (com.intellij.psi.codeStyle.extractor.values.ValuesExtractionResult)1 NotNull (org.jetbrains.annotations.NotNull)1