<commit_before>package com.intellij.help.impl;

import com.intellij.CommonBundle;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.plugins.HelpSetPath;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NonNls;

import javax.help.BadIDException;
import javax.help.HelpSet;
import javax.help.HelpSetException;
import java.awt.*;
import java.net.URL;

public class HelpManagerImpl extends HelpManager {
  private HelpSet myHelpSet = null;
  private IdeaHelpBroker myBroker = null;
  @NonNls private static final String HELP_HS = "Help.hs";

  public void invokeHelp(String id) {
    if (myHelpSet == null) {
      try {
        myHelpSet = createHelpSet();
      } catch (Exception ex) {
      }
    }
    if (myHelpSet == null) {
      Messages.showMessageDialog(IdeBundle.message("help.not.found.error", id),
                                 IdeBundle.message("help.not.found.title"), Messages.getErrorIcon());
      return;
    }
    if (myBroker == null) {
      myBroker = new IdeaHelpBroker(myHelpSet);
    }

    Window activeWindow=KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    myBroker.setActivationWindow(activeWindow);
    /*
    URL currentURL = myBroker.getCurrentURL();
    System.out.println("currentURL = " + currentURL);
    */
    if (id != null) {
      try {
        myBroker.setCurrentID(id);
      }
      catch (BadIDException e) {
        Messages.showMessageDialog(IdeBundle.message("help.topic.not.found.error", id),
                                   CommonBundle.getErrorTitle(), Messages.getErrorIcon());
        return;
      }
    }
    myBroker.setDisplayed(true);
  }

  private static HelpSet createHelpSet() {
    String urlToHelp = ApplicationInfo.getInstance().getHelpURL() + "/" + HELP_HS;

    try {
      HelpSet helpSet = new HelpSet(null, new URL (urlToHelp));

      // merge plugins help sets
      final Application app = ApplicationManager.getApplication();
      IdeaPluginDescriptor[] pluginDescriptors = app.getPlugins();
      for (int i = 0; i < pluginDescriptors.length; i++) {
        IdeaPluginDescriptor pluginDescriptor = pluginDescriptors[i];
        if (pluginDescriptor.getHelpSets() != null && pluginDescriptor.getHelpSets().length > 0) {
          for (int j = 0; j < pluginDescriptor.getHelpSets().length; j++) {
            HelpSetPath hsPath = pluginDescriptor.getHelpSets()[j];

            URL hsURL = new URL("jar:file:///" + pluginDescriptor.getPath().getAbsolutePath() + "/help/" + hsPath.getFile() + "!" + hsPath.getPath());
            try {
              HelpSet pluginHelpSet = new HelpSet (null, hsURL);
              helpSet.add(pluginHelpSet);
            }
            catch (HelpSetException e) {
              e.printStackTrace();
              // damn
            }
          }
        }
      }

      return helpSet;
    }
    catch (Exception ee) {
      System.err.println("HelpSet " + urlToHelp + " not found");
      return null;
    }
  }
}
<commit_msg>Default to Web help if no bundled help jar found.<commit_after>package com.intellij.help.impl;

import com.intellij.CommonBundle;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.plugins.HelpSetPath;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.help.BadIDException;
import javax.help.HelpSet;
import javax.help.HelpSetException;
import java.awt.*;
import java.net.URL;

public class HelpManagerImpl extends HelpManager {
  private static final Logger LOG = Logger.getInstance("#com.intellij.help.impl.HelpManagerImpl");

  private HelpSet myHelpSet = null;
  private IdeaHelpBroker myBroker = null;
  @NonNls private static final String HELP_HS = "Help.hs";

  public void invokeHelp(String id) {
    if (myHelpSet == null) {
      try {
        myHelpSet = createHelpSet();
      }
      catch (Exception ex) {
        // Ignore, will fallback to use web help
      }
    }

    if (myHelpSet == null) {
      BrowserUtil.launchBrowser("http://www.jetbrains.com/idea/webhelp/?" + id);
      return;
    }
    
    if (myBroker == null) {
      myBroker = new IdeaHelpBroker(myHelpSet);
    }

    Window activeWindow=KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    myBroker.setActivationWindow(activeWindow);

    if (id != null) {
      try {
        myBroker.setCurrentID(id);
      }
      catch (BadIDException e) {
        Messages.showMessageDialog(IdeBundle.message("help.topic.not.found.error", id),
                                   CommonBundle.getErrorTitle(), Messages.getErrorIcon());
        return;
      }
    }
    myBroker.setDisplayed(true);
  }

  @Nullable
  private static HelpSet createHelpSet() {
    String urlToHelp = ApplicationInfo.getInstance().getHelpURL() + "/" + HELP_HS;

    try {
      HelpSet helpSet = new HelpSet(null, new URL (urlToHelp));

      // merge plugins help sets
      final Application app = ApplicationManager.getApplication();
      IdeaPluginDescriptor[] pluginDescriptors = app.getPlugins();
      for (IdeaPluginDescriptor pluginDescriptor : pluginDescriptors) {
        HelpSetPath[] sets = pluginDescriptor.getHelpSets();
        for (HelpSetPath hsPath : sets) {
          URL hsURL =
            new URL("jar:file:///" + pluginDescriptor.getPath().getAbsolutePath() + "/help/" + hsPath.getFile() + "!" + hsPath.getPath());
          try {
            HelpSet pluginHelpSet = new HelpSet(null, hsURL);
            helpSet.add(pluginHelpSet);
          }
          catch (HelpSetException e) {
            LOG.error(e);
          }
        }
      }

      return helpSet;
    }
    catch (Exception ee) {
      return null;
    }
  }
}

{'fraction_non_alphanumeric': 0.11296412838263059, 'fraction_numerical': 0.0004719949653870359, 'mean_word_length': 2.890452876376989, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013412', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1869, 'n_words': 372}