From 0bcb25ec221746efde88dacbb5bea0e1e603e96b Mon Sep 17 00:00:00 2001 From: Timotei Dolean Date: Wed, 29 Jun 2011 18:48:33 +0000 Subject: [PATCH] eclipse plugin: Refactor the SchemaParser to take in account the install --- .../ui/contentassist/WMLProposalProvider.java | 73 ++++++++--------- .../ui/labeling/wmldoc/WMLDocHandler.java | 7 +- .../wesnoth/ui/labeling/wmldoc/WMLDocTag.java | 6 +- .../src/org/wesnoth/WMLStandaloneSetup.java | 3 +- .../wesnoth/validation/WMLJavaValidator.java | 10 ++- .../wesnoth/handlers/ReloadFilesHandler.java | 3 +- .../org/wesnoth/preferences/Preferences.java | 4 + .../src/org/wesnoth/schema/SchemaParser.java | 81 +++++++++++-------- .../wizards/generator/WizardGenerator.java | 6 +- 9 files changed, 106 insertions(+), 87 deletions(-) diff --git a/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java b/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java index 5dd9efcefea..48ef0fafd32 100644 --- a/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java +++ b/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java @@ -22,8 +22,9 @@ import org.eclipse.xtext.parsetree.LeafNode; import org.eclipse.xtext.parsetree.NodeUtil; import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext; import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor; -import org.wesnoth.Logger; +import org.wesnoth.installs.WesnothInstallsUtils; import org.wesnoth.preprocessor.Define; +import org.wesnoth.projects.ProjectCache; import org.wesnoth.projects.ProjectUtils; import org.wesnoth.schema.SchemaParser; import org.wesnoth.schema.Tag; @@ -40,20 +41,31 @@ import org.wesnoth.wml.impl.WmlFactoryImpl; @SuppressWarnings("unused") public class WMLProposalProvider extends AbstractWMLProposalProvider { - /** - * We have the following priorities: - * - * 1700 - key values - * 1500 - key names - * 1000 - tags - * 100 - macro calls - */ + protected SchemaParser schemaParser_; + protected ProjectCache projectCache_; + protected static final int KEY_VALUE_PRIORITY = 1700; + protected static final int KEY_NAME_PRIORITY = 1500; + protected static final int TAG_PRIORITY = 1000; + protected static final int MACRO_CALL_PRIORITY = 100; + + /** + * For priorities, see: + * {@link #KEY_NAME_PRIORITY} + * {@link #KEY_VALUE_PRIORITY} + * {@link #TAG_PRIORITY} + * {@link #MACRO_CALL_PRIORITY} + */ public WMLProposalProvider() { super(); + + IFile file = WMLUtil.getActiveEditorFile(); + projectCache_ = ProjectUtils.getCacheForProject( file.getProject( ) ); + // load the schema so we know what to suggest for autocomplete - SchemaParser.getInstance().parseSchema(false); + SchemaParser.reloadSchemas( false ); + schemaParser_ = SchemaParser.getInstance( WesnothInstallsUtils.getInstallNameForResource( file ) ); } @Override @@ -113,15 +125,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider private void addMacroCallProposals(EObject model, boolean ruleProposal, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { - IFile file = WMLUtil.getActiveEditorFile(); - if (file == null) - { - Logger.getInstance().logError("FATAL! file is null (and it shouldn't)"); //$NON-NLS-1$ - return; - } - - for(Entry define : ProjectUtils.getCacheForProject( - file.getProject()).getDefines().entrySet()) + for(Entry define : projectCache_.getDefines().entrySet()) { StringBuilder proposal = new StringBuilder(10); if (ruleProposal == true) @@ -133,7 +137,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider proposal.append("}"); //$NON-NLS-1$ acceptor.accept(createCompletionProposal(proposal.toString(), define.getKey(), - WMLLabelProvider.getImageByName("macrocall.png"), context, 100)); //$NON-NLS-1$ + WMLLabelProvider.getImageByName("macrocall.png"), context, MACRO_CALL_PRIORITY)); //$NON-NLS-1$ } } @@ -155,21 +159,13 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider if (key.getName().equals("next_scenario") || //$NON-NLS-1$ key.getName().equals("first_scenario")) //$NON-NLS-1$ { - IFile file = WMLUtil.getActiveEditorFile(); - if (file == null) - { - Logger.getInstance().logError("FATAL! file is null (and it shouldn't)"); //$NON-NLS-1$ - return; - } - - for(ConfigFile config : ProjectUtils. - getCacheForProject(file.getProject()).getConfigs().values()) + for(ConfigFile config : projectCache_.getConfigs().values()) { if (StringUtils.isNullOrEmpty( config.ScenarioId )) continue; acceptor.accept(createCompletionProposal(config.ScenarioId, config.ScenarioId, WMLLabelProvider.getImageByName("scenario.png"), //$NON-NLS-1$ - context, 1700)); + context, KEY_VALUE_PRIORITY)); } } else @@ -178,7 +174,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider model.eContainer() instanceof WMLTag) { WMLTag parent = (WMLTag) model.eContainer(); - Tag tag = SchemaParser.getInstance().getTags().get(parent.getName()); + Tag tag = schemaParser_.getTags().get(parent.getName()); if (tag != null) { TagKey tagKey = tag.getChildKey(key.getName()); @@ -213,11 +209,11 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider if (tag != null) { - if (SchemaParser.getInstance(). getTags().get(tag.getName()) != null) + Tag schemaTag = schemaParser_.getTags().get(tag.getName()); + if ( schemaTag != null) { boolean found = false; - for(TagKey key : SchemaParser.getInstance(). - getTags().get(tag.getName()).getKeyChildren()) + for(TagKey key : schemaTag.getKeyChildren()) { // skip forbidden keys if (key.isForbidden()) @@ -237,7 +233,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider acceptor.accept(createCompletionProposal(key.getName() + "=", //$NON-NLS-1$ key.getName(), getImage(WmlFactoryImpl.eINSTANCE.getWmlPackage().getWMLKey()), - context, 1500)); + context, KEY_NAME_PRIORITY)); } } } @@ -274,7 +270,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider // remove ugly new lines that break indentation parentIndent = parentIndent.replace("\r", "").replace("\n", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - Tag tagChildren = SchemaParser.getInstance().getTags().get(parentTag.getName()); + Tag tagChildren = schemaParser_.getTags().get(parentTag.getName()); if (tagChildren != null) { boolean found = false; @@ -307,7 +303,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider } else // we are at the root { - Tag rootTag = SchemaParser.getInstance().getTags().get("root"); //$NON-NLS-1$ + Tag rootTag = schemaParser_.getTags().get("root"); //$NON-NLS-1$ dbg("root node. adding tags: "+ rootTag.getTagChildren().size()); //$NON-NLS-1$ for(Tag tag : rootTag.getTagChildren()) { @@ -327,7 +323,6 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider private ICompletionProposal tagProposal(Tag tag, String indent, boolean ruleProposal, ContentAssistContext context) { -// dbg("indent:[" + indent +"]"); StringBuilder proposal = new StringBuilder(); if (ruleProposal) proposal.append("["); //$NON-NLS-1$ @@ -342,7 +337,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider proposal.append(String.format("%s[/%s]",indent, tag.getName())); //$NON-NLS-1$ return createCompletionProposal(proposal.toString(), tag.getName(), getImage(WmlFactoryImpl.eINSTANCE.getWmlPackage().getWMLTag()), - context, 100); + context, TAG_PRIORITY); } private ICompletionProposal createCompletionProposal(String proposal, diff --git a/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocHandler.java b/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocHandler.java index b629ab39301..5dbe640375f 100644 --- a/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocHandler.java +++ b/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocHandler.java @@ -22,13 +22,13 @@ import org.eclipse.xtext.ui.editor.XtextEditor; import org.eclipse.xtext.ui.editor.utils.EditorUtils; import org.eclipse.xtext.util.concurrent.IUnitOfWork; import org.wesnoth.Logger; +import org.wesnoth.installs.WesnothInstallsUtils; import org.wesnoth.preprocessor.Define; import org.wesnoth.projects.ProjectUtils; import org.wesnoth.ui.WMLUtil; import org.wesnoth.wml.WMLMacroCall; import org.wesnoth.wml.WMLTag; - /** * A handler that handles pressing F2 on a resource in the editor */ @@ -39,6 +39,9 @@ public class WMLDocHandler extends AbstractHandler try { final XtextEditor editor = EditorUtils.getActiveXtextEditor(event); + final String installName = + WesnothInstallsUtils.getInstallNameForResource( WMLUtil.getActiveEditorFile( ) ); + editor.getDocument().readOnly(new IUnitOfWork.Void() { private WMLDocInformationPresenter presenter_; @@ -81,7 +84,7 @@ public class WMLDocHandler extends AbstractHandler { presenter_ = new WMLDocInformationPresenter( editor.getSite().getShell(), - new WMLDocTag(((WMLTag)container.getElement()).getName()), + new WMLDocTag( installName, ((WMLTag)container.getElement()).getName()), positionAbsolute); presenter_.create(); } diff --git a/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocTag.java b/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocTag.java index 85d7fbf06d6..bf2241603e6 100644 --- a/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocTag.java +++ b/utils/umc_dev/org.wesnoth.wml.ui/src/org/wesnoth/ui/labeling/wmldoc/WMLDocTag.java @@ -18,7 +18,6 @@ import org.wesnoth.schema.SchemaParser; import org.wesnoth.schema.Tag; import org.wesnoth.ui.Messages; - /** * Displays wml doc for a tag * [tag] or [/tag] @@ -30,10 +29,11 @@ public class WMLDocTag implements IWMLDocProvider private String contents_; private List styleRanges_; - public WMLDocTag(String name) + public WMLDocTag( String installName, String name ) { - tag_ = SchemaParser.getInstance().getTags().get(name); + tag_ = SchemaParser.getInstance( installName ).getTags().get(name); } + /** * A method used for lazly generating the documentation */ diff --git a/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/WMLStandaloneSetup.java b/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/WMLStandaloneSetup.java index e69520e10b0..c874e69932a 100644 --- a/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/WMLStandaloneSetup.java +++ b/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/WMLStandaloneSetup.java @@ -18,7 +18,8 @@ public class WMLStandaloneSetup extends WMLStandaloneSetupGenerated { public static void doSetup() { new WMLStandaloneSetup().createInjectorAndDoEMFRegistration(); - SchemaParser.getInstance().parseSchema(false); + + SchemaParser.reloadSchemas( false ); } } diff --git a/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/validation/WMLJavaValidator.java b/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/validation/WMLJavaValidator.java index a2bca0eadec..47fbddc3b78 100644 --- a/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/validation/WMLJavaValidator.java +++ b/utils/umc_dev/org.wesnoth.wml/src/org/wesnoth/validation/WMLJavaValidator.java @@ -21,7 +21,6 @@ import org.wesnoth.wml.WMLRoot; import org.wesnoth.wml.WMLTag; import org.wesnoth.wml.WmlPackage; - /** * This represents the validator for config files * @@ -56,10 +55,13 @@ public class WMLJavaValidator extends AbstractWMLJavaValidator { searchName = "root"; //$NON-NLS-1$ } - if (SchemaParser.getInstance().getTags().get(searchName) != null) + + //TODO: get the editor based on this validator?? + Tag schemaTag = SchemaParser.getInstance( null ).getTags().get(searchName); + + if ( schemaTag != null ) { - for(Tag childTag : SchemaParser.getInstance().getTags().get(searchName). - getTagChildren()) + for(Tag childTag : schemaTag.getTagChildren()) { if (childTag.getName().equals(tag.getName())) { diff --git a/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/ReloadFilesHandler.java b/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/ReloadFilesHandler.java index 54f0e0d7c93..20f7ac17d5f 100644 --- a/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/ReloadFilesHandler.java +++ b/utils/umc_dev/org.wesnoth/src/org/wesnoth/handlers/ReloadFilesHandler.java @@ -20,13 +20,12 @@ import org.wesnoth.schema.SchemaParser; import org.wesnoth.templates.TemplateProvider; import org.wesnoth.utils.GUIUtils; - public class ReloadFilesHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) { - SchemaParser.getInstance().parseSchema(true); + SchemaParser.reloadSchemas( true ); TemplateProvider.getInstance().loadTemplates(); diff --git a/utils/umc_dev/org.wesnoth/src/org/wesnoth/preferences/Preferences.java b/utils/umc_dev/org.wesnoth/src/org/wesnoth/preferences/Preferences.java index 1f6221e426f..f695125134f 100644 --- a/utils/umc_dev/org.wesnoth/src/org/wesnoth/preferences/Preferences.java +++ b/utils/umc_dev/org.wesnoth/src/org/wesnoth/preferences/Preferences.java @@ -139,6 +139,10 @@ public class Preferences extends AbstractPreferenceInitializer */ public static Paths getPaths( String installName ) { + // no null allowed -> fallback to "" + if ( installName == null ) + installName = ""; + Paths paths = paths_.get( installName ); if ( paths == null ) { paths = new Paths( getInstallPrefix( installName ) ); diff --git a/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/SchemaParser.java b/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/SchemaParser.java index 31ff5e1eab5..3c74ed311d3 100644 --- a/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/SchemaParser.java +++ b/utils/umc_dev/org.wesnoth/src/org/wesnoth/schema/SchemaParser.java @@ -11,34 +11,70 @@ package org.wesnoth.schema; import java.io.File; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Stack; import org.wesnoth.Logger; import org.wesnoth.Messages; +import org.wesnoth.installs.WesnothInstall; +import org.wesnoth.installs.WesnothInstallsUtils; import org.wesnoth.preferences.Preferences; import org.wesnoth.utils.ResourceUtils; import org.wesnoth.utils.StringUtils; - /** * This is a 'schema.cfg' parser. */ public class SchemaParser { - //TODO: add a faster search method for keys/tags by name - private static SchemaParser instance_; + protected static Map< String, SchemaParser > parsers_ = + new HashMap(); - public static SchemaParser getInstance() + /** + * Returns a SchemaParser instance based on the specified install + * @param installName The name of the install + * @return A SchemaParser singleton instance + */ + public static SchemaParser getInstance( String installName ) { - if (instance_ == null) - instance_ = new SchemaParser(); - return instance_; + // null not allowed + if ( installName == null ) + installName = ""; + + SchemaParser parser = parsers_.get( installName ); + if (parser == null) { + parser = new SchemaParser( installName ); + parsers_.put( installName, parser ); + } + + return parser; } - private Map primitives_ = new HashMap(); - private Map tags_ = new HashMap(); - private boolean parsingDone_ = false; + /** + * Reloads all currently stored schemas + * @param force True to force reloading schemas + */ + public static void reloadSchemas( boolean force ) + { + List< WesnothInstall > installs = WesnothInstallsUtils.getInstalls( ); + for ( WesnothInstall install : installs ) { + getInstance( install.getName( ) ).parseSchema( force ); + } + } + + private Map primitives_; + private Map tags_; + private boolean parsingDone_; + private String installName_; + + private SchemaParser( String installName ) + { + installName_ = installName; + primitives_ = new HashMap(); + tags_ = new HashMap(); + parsingDone_ = false; + } /** * Parses the schema @@ -50,9 +86,7 @@ public class SchemaParser */ public void parseSchema( boolean force ) { - //TODO should parse schema for each install type - // for now, use the default install - parseSchemaFile( force, Preferences.getPaths( null ).getSchemaPath( ) ); + parseSchemaFile( force, Preferences.getPaths( installName_ ).getSchemaPath( ) ); } /** @@ -254,27 +288,6 @@ public class SchemaParser Logger.getInstance().log(Messages.SchemaParser_36); parsingDone_ = true; - -// try -// { -// BufferedWriter bw = new BufferedWriter(new PrintWriter(new File("E:/work/gw/data/schema-out.cfg"))); -// // print primitives -// for (Entry primitive : primitives_.entrySet()) -// { -// bw.write(primitive.getKey() + ": " + primitive.getValue() + "\n"); -// } -// // print tags -// Tag root = tags_.get("root"); -// for (Tag tag : root.getTagChildren()) -// { -// bw.write(getOutput(tag, 0)); -// } -// bw.close(); -// } catch (Exception e) -// { -// Logger.getInstance().logException(e); -// } -// System.out.println("End writing result"); } /** diff --git a/utils/umc_dev/org.wesnoth/src/org/wesnoth/wizards/generator/WizardGenerator.java b/utils/umc_dev/org.wesnoth/src/org/wesnoth/wizards/generator/WizardGenerator.java index d739c5e1cc2..b39f0735016 100644 --- a/utils/umc_dev/org.wesnoth/src/org/wesnoth/wizards/generator/WizardGenerator.java +++ b/utils/umc_dev/org.wesnoth/src/org/wesnoth/wizards/generator/WizardGenerator.java @@ -22,9 +22,11 @@ public class WizardGenerator extends NewWizardTemplate private byte indent_; public WizardGenerator(String title, String tagName, byte indent) { - SchemaParser.getInstance().parseSchema(false); + + // TODO: wizards should ask the install + SchemaParser.getInstance( null ).parseSchema(false); setWindowTitle(title); - Tag tagContent = SchemaParser.getInstance().getTags().get(tagName); + Tag tagContent = SchemaParser.getInstance( null ).getTags().get(tagName); tagName_ = tagName; indent_ = indent;