Changeset 1205


Ignore:
Timestamp:
Jul 11, 2011, 11:01:50 PM (6 years ago)
Author:
stefan
Message:

added package import suggestion for environments

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG

    r1194 r1205  
    1010
    1111* new features:
    12   - suggestions for package imports via alt+ENTER on undeclared commands
     12  - suggestions for package imports via alt+ENTER on undefined commands or environment
    1313  - quick help shows lists of packages and documentclasses providing command under cursor
    1414  - option to create macro from existing LaTeX code via control+shift+M or "LaTeX"->"Extract Command"
  • trunk/src/jlatexeditor/PackagesExtractor.java

    r1200 r1205  
    6767    private Trie<Package> packages = new Trie<Package>();
    6868    private TrieSet<Command> commands = new TrieSet<Command>();
     69    private TrieSet<Environment> environments = new TrieSet<Environment>();
    6970
    7071    public PackageParser(String fileName) {
     
    9293        commands.add(name, command);
    9394      } else
     95      if (localName.equals("environment")) {
     96        String name = attrList.getValue("name");
     97        int argCount = Integer.parseInt(attrList.getValue("argCount"));
     98        String optionalArg = attrList.getValue("optionalArg");
     99        Environment environment = new Environment(name, argCount, optionalArg, pack);
     100        environments.add(name, environment);
     101      } else
    94102      if (localName.equals("package")) {
    95103        pack = new Package(attrList.getValue("name"), attrList.getValue("options"), attrList.getValue("requiresPackages"), attrList.getValue("title"), attrList.getValue("description"), attrList.getValue("debPackage"), attrList.getValue("usageCount"));
     
    116124    public TrieSet<Command> getCommands() {
    117125      return commands;
     126    }
     127
     128    public TrieSet<Environment> getEnvironments() {
     129      return environments;
    118130    }
    119131
     
    249261  }
    250262
    251   public static class Command implements Comparable<Command> {
    252     private String name;
    253     private int argCount;
    254     private String optionalArg;
    255     private Package pack;
    256 
    257     public Command(String name, int argCount, String optionalArg, Package pack) {
     263  public abstract static class ComEnv implements Comparable<ComEnv> {
     264    protected String name;
     265    protected int argCount;
     266    protected String optionalArg;
     267    protected Package pack;
     268
     269    public ComEnv(String name, int argCount, String optionalArg, Package pack) {
    258270      this.name = name;
    259271      this.argCount = argCount;
     
    264276    @Override
    265277    public boolean equals(Object obj) {
    266       if (obj instanceof Command) {
    267         Command that = (Command) obj;
     278      if (obj instanceof ComEnv) {
     279        ComEnv that = (ComEnv) obj;
    268280        return this.name.equals(that.name) && this.pack.equals(that.pack);
    269281      }
     
    293305
    294306    @Override
    295     public int compareTo(Command that) {
     307    public int compareTo(ComEnv that) {
    296308      return this.name.compareTo(that.name);
    297309    }
    298310  }
     311
     312  public static class Command extends ComEnv {
     313    public Command(String name, int argCount, String optionalArg, Package pack) {
     314      super(name, argCount, optionalArg, pack);
     315    }
     316  }
     317
     318  public static class Environment extends ComEnv {
     319    public Environment(String name, int argCount, String optionalArg, Package pack) {
     320      super(name, argCount, optionalArg, pack);
     321    }
     322  }
    299323}
  • trunk/src/jlatexeditor/codehelper/BackgroundParser.java

    r1185 r1205  
    33import jlatexeditor.Doc;
    44import jlatexeditor.JLatexEditorJFrame;
     5import jlatexeditor.PackagesExtractor;
    56import sce.component.AbstractResource;
    67import sce.component.SourceCodeEditor;
     
    6162  }
    6263
     64  /**
     65   * Returns all imported packages as Trie.
     66   *
     67   * @return all imported packages as Trie
     68   */
    6369  public Trie<Package> getPackages() {
    6470    return stableState.packages;
     71  }
     72
     73  /**
     74   * Returns a HashSet with all directly and indirectly imported packages.
     75   *
     76   * @return HashSet with all directly and indirectly imported packages
     77   */
     78  public HashSet<PackagesExtractor.Package> getIndirectlyImportedPackages() {
     79    return stableState.getIndirectlyImportedPackages();
    6580  }
    6681
     
    487502
    488503    ArrayList<TODO> todos = new ArrayList<TODO>();
     504
     505    private HashSet<PackagesExtractor.Package> indirectlyImportedPackages = null;
     506
     507    public HashSet<PackagesExtractor.Package> getIndirectlyImportedPackages() {
     508      if (indirectlyImportedPackages == null) {
     509        // evaluate lazy value
     510        indirectlyImportedPackages = new HashSet<PackagesExtractor.Package>();
     511        // add packages directly or indirectly imported by imported packages
     512        for (Package pack : packages) {
     513          PackagesExtractor.Package aPackage = PackagesExtractor.getPackageParser().getPackages().get(pack.getName());
     514          if (aPackage != null) {
     515            aPackage.addRequiredPackagesRecursively(indirectlyImportedPackages);
     516          }
     517        }
     518        // add packages directly or indirectly imported by documentclass
     519        if (documentClass != null) {
     520          PackagesExtractor.Package aPackage = PackagesExtractor.getDocClassesParser().getPackages().get(documentClass.getName());
     521          aPackage.addRequiredPackagesRecursively(indirectlyImportedPackages);
     522        }
     523      }
     524      return indirectlyImportedPackages;
     525    }
    489526  }
    490527}
  • trunk/src/jlatexeditor/codehelper/PackageImportSuggester.java

    r1200 r1205  
    88import sce.component.SCEPane;
    99import sce.component.SourceCodeEditor;
     10import sun.print.BackgroundLookupListener;
    1011
    1112import java.io.File;
     
    1819 */
    1920public class PackageImportSuggester implements CodeAssistant, SCEPopup.ItemHandler {
    20   public static final PatternPair wordPattern = new PatternPair("\\\\(\\p{L}*)", "(\\p{L}*)");
     21  public static final PatternPair commandPattern = new PatternPair("\\\\(\\p{L}*)", "(\\p{L}*)");
     22  public static final PatternPair environmentPattern = new PatternPair("\\\\begin\\{(\\p{L}*)", "(\\p{L}*)\\}");
    2123
    2224  private SCEManagerInteraction jle;
     
    3032  public boolean assistAt(SCEPane pane) {
    3133    // get the word under the caret
    32     List<WordWithPos> wordList = wordPattern.find(pane);
    33 
    34     if (wordList == null) return false;
     34    List<WordWithPos> wordList = commandPattern.find(pane);
     35
     36    boolean isCommand = wordList != null;
     37
     38    if (!isCommand) {
     39      wordList = environmentPattern.find(pane);
     40
     41      if (wordList == null) return false;
     42    }
    3543
    3644    wordUnderCaret = wordList.get(0);
    37     String commandName = wordUnderCaret.word;
    38     if(commandName.length() == 0) return false;
     45    String comEnvName = wordUnderCaret.word;
     46    if(comEnvName.length() == 0) return false;
    3947
    4048    ArrayList<Object> suggestionList = new ArrayList<Object>();
     
    4452      String docClass = backgroundParser.getDocumentClass().getName();
    4553
    46       HashSet<PackagesExtractor.Command> commands = PackagesExtractor.getDocClassesParser().getCommands().get(commandName);
    47 
    48       if (commands != null) {
    49         for (PackagesExtractor.Command command : commands) {
    50           if (command.getPack().getName().equals(docClass)) {
    51             suggestionList.add("<html><body bgcolor='#202080'><table><tr><td width='500'><font color='#ffff00'>already provided</font> <font color='#A0A0A0'>by documentclass</font> <font color='#ffffff'>" + command.getPack().getName());
     54      HashSet<? extends PackagesExtractor.ComEnv> comEnvs;
     55      if (isCommand) {
     56        comEnvs = PackagesExtractor.getDocClassesParser().getCommands().get(comEnvName);
     57      } else {
     58        comEnvs = PackagesExtractor.getDocClassesParser().getEnvironments().get(comEnvName);
     59      }
     60
     61      // is document class providing the command / environment
     62      if (comEnvs != null) {
     63        for (PackagesExtractor.ComEnv comEnv : comEnvs) {
     64          if (comEnv.getPack().getName().equals(docClass)) {
     65            suggestionList.add("<html><body bgcolor='#202080'><table><tr><td width='500'><font color='#ffff00'>already provided</font> <font color='#A0A0A0'>by documentclass</font> <font color='#ffffff'>" + comEnv.getPack().getName());
    5266            break;
    5367          }
     
    5670    }
    5771
    58     HashSet<PackagesExtractor.Command> commands = PackagesExtractor.getPackageParser().getCommands().get(commandName);
    59 
    60     if (commands != null) {
     72    HashSet<? extends PackagesExtractor.ComEnv> comEnvs;
     73    if (isCommand) {
     74      comEnvs = PackagesExtractor.getPackageParser().getCommands().get(comEnvName);
     75    } else {
     76      comEnvs = PackagesExtractor.getPackageParser().getEnvironments().get(comEnvName);
     77    }
     78
     79    if (comEnvs != null) {
    6180      // build HashSet with all packages directly and indirectly imported in this document
    62       HashSet<PackagesExtractor.Package> directlyImportedPackagesHash = new HashSet<PackagesExtractor.Package>();
    63       HashSet<PackagesExtractor.Package> indirectlyImportedPackagesHash = new HashSet<PackagesExtractor.Package>();
    64       for (Package pack : backgroundParser.getPackages()) {
    65         PackagesExtractor.Package aPackage = PackagesExtractor.getPackageParser().getPackages().get(pack.getName());
    66         if (aPackage != null) {
    67           directlyImportedPackagesHash.add(aPackage);
    68           aPackage.addRequiredPackagesRecursively(indirectlyImportedPackagesHash);
    69         }
    70       }
     81      HashSet<PackagesExtractor.Package> indirectlyImportedPackagesHash = backgroundParser.getIndirectlyImportedPackages();
    7182
    7283      // build HashSet with all packages directly or indirectly providing the given command
    7384      HashSet<PackagesExtractor.Package> dependentPackagesHash = new HashSet<PackagesExtractor.Package>();
    74       for (PackagesExtractor.Command command : commands) {
    75         command.getPack().addDependantPackagesRecursively(dependentPackagesHash);
     85      for (PackagesExtractor.ComEnv comEnv : comEnvs) {
     86        comEnv.getPack().addDependantPackagesRecursively(dependentPackagesHash);
    7687      }
    7788
Note: See TracChangeset for help on using the changeset viewer.