<commit_before>/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * 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 com.intellij.notification;

import com.intellij.execution.filters.HyperlinkInfo;
import com.intellij.execution.impl.ConsoleViewUtil;
import com.intellij.execution.impl.EditorHyperlinkSupport;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.icons.AllIcons;
import com.intellij.notification.impl.NotificationSettings;
import com.intellij.notification.impl.NotificationsConfigurationImpl;
import com.intellij.notification.impl.NotificationsManagerImpl;
import com.intellij.notification.impl.ui.NotificationsUtil;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.colors.EditorColorsListener;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.colors.impl.DelegateColorScheme;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.editor.ex.*;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.editor.impl.ContextMenuPopupHandler;
import com.intellij.openapi.editor.markup.*;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerAdapter;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColorUtil;
import com.intellij.ui.JBColor;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.DateFormatUtil;
import consulo.ui.UIAccess;
import consulo.util.dataholder.Key;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author peter
 */
class EventLogConsole {
  private static final Key<String> GROUP_ID = Key.create("GROUP_ID");
  private static final Key<String> NOTIFICATION_ID = Key.create("NOTIFICATION_ID");

  private final NotNullLazyValue<Editor> myLogEditor = new NotNullLazyValue<Editor>() {
    @Nonnull
    @Override
    protected Editor compute() {
      return createLogEditor();
    }
  };

  private final NotNullLazyValue<EditorHyperlinkSupport> myHyperlinkSupport = new NotNullLazyValue<EditorHyperlinkSupport>() {
    @Nonnull
    @Override
    protected EditorHyperlinkSupport compute() {
      return new EditorHyperlinkSupport(getConsoleEditor(), myProjectModel.getProject());
    }
  };
  private final LogModel myProjectModel;

  private String myLastDate;

  private List<RangeHighlighter> myNMoreHighlighters;

  EventLogConsole(LogModel model) {
    myProjectModel = model;
  }

  private Editor createLogEditor() {
    Project project = myProjectModel.getProject();
    final EditorEx editor = ConsoleViewUtil.setupConsoleEditor(project, false, false);
    editor.getSettings().setFoldingOutlineShown(true);
    editor.getSettings().setWhitespacesShown(false);
    installNotificationsFont(editor);
    myProjectModel.getProject().getMessageBus().connect().subscribe(ProjectManager.TOPIC, new ProjectManagerAdapter() {
      @Override
      public void projectClosed(Project project, UIAccess uiAccess) {
        if (project == myProjectModel.getProject()) {
          EditorFactory.getInstance().releaseEditor(editor);
        }
      }
    });

    ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeVisible(true);

    final ClearLogAction clearLog = new ClearLogAction(this);
    clearLog.registerCustomShortcutSet(ActionManager.getInstance().getAction(IdeActions.CONSOLE_CLEAR_ALL).getShortcutSet(), editor.getContentComponent());

    editor.installPopupHandler(new ContextMenuPopupHandler() {
      @Nullable
      @Override
      public ActionGroup getActionGroup(@Nonnull EditorMouseEvent event) {
        final ActionManager actionManager = ActionManager.getInstance();
        return createPopupActions(actionManager, clearLog, editor, event);
      }
    });
    return editor;
  }

  private void installNotificationsFont(@Nonnull final EditorEx editor) {
    final DelegateColorScheme globalScheme = new DelegateColorScheme(EditorColorsManager.getInstance().getGlobalScheme()) {
      @Override
      public String getEditorFontName() {
        return getConsoleFontName();
      }

      @Override
      public int getEditorFontSize() {
        return getConsoleFontSize();
      }

      @Override
      public String getConsoleFontName() {
        return NotificationsUtil.getFontName();
      }

      @Override
      public int getConsoleFontSize() {
        Pair<String, Integer> data = NotificationsUtil.getFontData();
        return data == null ? super.getConsoleFontSize() : data.second;
      }

      @Override
      public void setEditorFontName(String fontName) {
      }

      @Override
      public void setConsoleFontName(String fontName) {
      }

      @Override
      public void setEditorFontSize(int fontSize) {
      }

      @Override
      public void setConsoleFontSize(int fontSize) {
      }
    };
    EditorColorsManager.getInstance().addEditorColorsListener(new EditorColorsListener() {
      @Override
      public void globalSchemeChange(EditorColorsScheme scheme) {
        globalScheme.setDelegate(EditorColorsManager.getInstance().getGlobalScheme());
        editor.reinitSettings();
      }
    }, myProjectModel::dispose);
    editor.setColorsScheme(ConsoleViewUtil.updateConsoleColorScheme(editor.createBoundColorSchemeDelegate(globalScheme)));
  }

  private static DefaultActionGroup createPopupActions(ActionManager actionManager, ClearLogAction action, EditorEx editor, EditorMouseEvent event) {
    AnAction[] children = ((ActionGroup)actionManager.getAction(IdeActions.GROUP_CONSOLE_EDITOR_POPUP)).getChildren(null);
    DefaultActionGroup group = new DefaultActionGroup();
    addConfigureNotificationAction(editor, event, group);
    group.addAll(children);
    group.addSeparator();
    group.add(action);
    return group;
  }

  private static void addConfigureNotificationAction(@Nonnull EditorEx editor, @Nonnull EditorMouseEvent event, @Nonnull final DefaultActionGroup actions) {
    LogicalPosition position = editor.xyToLogicalPosition(event.getMouseEvent().getPoint());
    if (EditorUtil.inVirtualSpace(editor, position)) {
      return;
    }
    int offset = editor.logicalPositionToOffset(position);
    editor.getMarkupModel().processRangeHighlightersOverlappingWith(offset, offset, new Processor<RangeHighlighterEx>() {
      @Override
      public boolean process(RangeHighlighterEx rangeHighlighter) {
        String groupId = GROUP_ID.get(rangeHighlighter);
        if (groupId != null) {<commit_msg>use correct disposable parent<commit_after>/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * 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 com.intellij.notification;

import com.intellij.execution.filters.HyperlinkInfo;
import com.intellij.execution.impl.ConsoleViewUtil;
import com.intellij.execution.impl.EditorHyperlinkSupport;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.icons.AllIcons;
import com.intellij.notification.impl.NotificationSettings;
import com.intellij.notification.impl.NotificationsConfigurationImpl;
import com.intellij.notification.impl.NotificationsManagerImpl;
import com.intellij.notification.impl.ui.NotificationsUtil;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.colors.EditorColorsListener;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.colors.impl.DelegateColorScheme;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.editor.ex.*;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.editor.impl.ContextMenuPopupHandler;
import com.intellij.openapi.editor.markup.*;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerAdapter;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColorUtil;
import com.intellij.ui.JBColor;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.DateFormatUtil;
import consulo.ui.UIAccess;
import consulo.util.dataholder.Key;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author peter
 */
class EventLogConsole {
  private static final Key<String> GROUP_ID = Key.create("GROUP_ID");
  private static final Key<String> NOTIFICATION_ID = Key.create("NOTIFICATION_ID");

  private final NotNullLazyValue<Editor> myLogEditor = new NotNullLazyValue<Editor>() {
    @Nonnull
    @Override
    protected Editor compute() {
      return createLogEditor();
    }
  };

  private final NotNullLazyValue<EditorHyperlinkSupport> myHyperlinkSupport = new NotNullLazyValue<EditorHyperlinkSupport>() {
    @Nonnull
    @Override
    protected EditorHyperlinkSupport compute() {
      return new EditorHyperlinkSupport(getConsoleEditor(), myProjectModel.getProject());
    }
  };
  private final LogModel myProjectModel;

  private String myLastDate;

  private List<RangeHighlighter> myNMoreHighlighters;

  EventLogConsole(LogModel model) {
    myProjectModel = model;
  }

  private Editor createLogEditor() {
    Project project = myProjectModel.getProject();
    final EditorEx editor = ConsoleViewUtil.setupConsoleEditor(project, false, false);
    editor.getSettings().setFoldingOutlineShown(true);
    editor.getSettings().setWhitespacesShown(false);
    installNotificationsFont(editor);
    myProjectModel.getProject().getMessageBus().connect().subscribe(ProjectManager.TOPIC, new ProjectManagerAdapter() {
      @Override
      public void projectClosed(Project project, UIAccess uiAccess) {
        if (project == myProjectModel.getProject()) {
          EditorFactory.getInstance().releaseEditor(editor);
        }
      }
    });

    ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeVisible(true);

    final ClearLogAction clearLog = new ClearLogAction(this);
    clearLog.registerCustomShortcutSet(ActionManager.getInstance().getAction(IdeActions.CONSOLE_CLEAR_ALL).getShortcutSet(), editor.getContentComponent());

    editor.installPopupHandler(new ContextMenuPopupHandler() {
      @Nullable
      @Override
      public ActionGroup getActionGroup(@Nonnull EditorMouseEvent event) {
        final ActionManager actionManager = ActionManager.getInstance();
        return createPopupActions(actionManager, clearLog, editor, event);
      }
    });
    return editor;
  }

  private void installNotificationsFont(@Nonnull final EditorEx editor) {
    final DelegateColorScheme globalScheme = new DelegateColorScheme(EditorColorsManager.getInstance().getGlobalScheme()) {
      @Override
      public String getEditorFontName() {
        return getConsoleFontName();
      }

      @Override
      public int getEditorFontSize() {
        return getConsoleFontSize();
      }

      @Override
      public String getConsoleFontName() {
        return NotificationsUtil.getFontName();
      }

      @Override
      public int getConsoleFontSize() {
        Pair<String, Integer> data = NotificationsUtil.getFontData();
        return data == null ? super.getConsoleFontSize() : data.second;
      }

      @Override
      public void setEditorFontName(String fontName) {
      }

      @Override
      public void setConsoleFontName(String fontName) {
      }

      @Override
      public void setEditorFontSize(int fontSize) {
      }

      @Override
      public void setConsoleFontSize(int fontSize) {
      }
    };
    EditorColorsManager.getInstance().addEditorColorsListener(new EditorColorsListener() {
      @Override
      public void globalSchemeChange(EditorColorsScheme scheme) {
        globalScheme.setDelegate(EditorColorsManager.getInstance().getGlobalScheme());
        editor.reinitSettings();
      }
    }, myProjectModel);
    editor.setColorsScheme(ConsoleViewUtil.updateConsoleColorScheme(editor.createBoundColorSchemeDelegate(globalScheme)));
  }

  private static DefaultActionGroup createPopupActions(ActionManager actionManager, ClearLogAction action, EditorEx editor, EditorMouseEvent event) {
    AnAction[] children = ((ActionGroup)actionManager.getAction(IdeActions.GROUP_CONSOLE_EDITOR_POPUP)).getChildren(null);
    DefaultActionGroup group = new DefaultActionGroup();
    addConfigureNotificationAction(editor, event, group);
    group.addAll(children);
    group.addSeparator();
    group.add(action);
    return group;
  }

  private static void addConfigureNotificationAction(@Nonnull EditorEx editor, @Nonnull EditorMouseEvent event, @Nonnull final DefaultActionGroup actions) {
    LogicalPosition position = editor.xyToLogicalPosition(event.getMouseEvent().getPoint());
    if (EditorUtil.inVirtualSpace(editor, position)) {
      return;
    }
    int offset = editor.logicalPositionToOffset(position);
    editor.getMarkupModel().processRangeHighlightersOverlappingWith(offset, offset, new Processor<RangeHighlighterEx>() {
      @Override
      public boolean process(RangeHighlighterEx rangeHighlighter) {
        String groupId = GROUP_ID.get(rangeHighlighter);
        if (groupId != null) {
{'fraction_non_alphanumeric': 0.09368986795833056, 'fraction_numerical': 0.0015924623449008028, 'mean_word_length': 4.653413353338334, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2409268', 'n_tokens_mistral': 4206, 'n_tokens_neox': 4142, 'n_words': 845}