Skip to content

Commit

Permalink
Closes #80 - Added parameterized default function and variable namesp…
Browse files Browse the repository at this point in the history
…ace per xquery flavour type.
  • Loading branch information
ligasgr committed May 25, 2014
1 parent 32e72ed commit ea69192
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 38 deletions.
38 changes: 38 additions & 0 deletions src/main/java/org/intellij/xquery/XQueryFlavour.java
@@ -0,0 +1,38 @@
/*
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors
* (see the CONTRIBUTORS file).
*
* 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 org.intellij.xquery;

public enum XQueryFlavour {
STANDARD_30("XQuery 3.0 Standard"),
BASEX("BaseX"),
EXIST("eXist"),
MARKLOGIC("MarkLogic"),
SAXON("Saxon"),
SEDNA("Sedna"),
ZORBA("Zorba");

private final String presentableName;

XQueryFlavour(String presentableName) {
this.presentableName = presentableName;
}

public String getPresentableName() {
return presentableName;
}
}
Expand Up @@ -50,13 +50,15 @@ public static XQueryQNameBuilder<XQueryFunctionName> aXQueryQName(XQueryFunction
}

public static XQueryQNameBuilder<XQueryVarName> aXQueryQName(XQueryVarName varName) {
XQueryFile containingFile = (XQueryFile) varName.getContainingFile();
XQueryQNameBuilder<XQueryVarName> instance = new XQueryQNameBuilder<XQueryVarName>();
if (varName.getPrefix() != null) {
instance.prefix = varName.getPrefix().getText();
}
if (varName.getVarLocalName() != null) {
instance.localName = varName.getVarLocalName().getText();
}
instance.namespace = containingFile.mapVariablePrefixToNamespace(instance.prefix);
instance.namedObject = varName;
return instance;
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/org/intellij/xquery/psi/XQueryFile.java
Expand Up @@ -26,7 +26,9 @@
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiTreeUtil;
import org.intellij.xquery.XQueryFileType;
import org.intellij.xquery.XQueryFlavour;
import org.intellij.xquery.XQueryLanguage;
import org.intellij.xquery.settings.XQuerySettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -290,6 +292,9 @@ private String calcDefaultFunctionNamespace() {
XQueryDefaultFunctionNamespaceDecl defaultFunctionNamespaceDecl = getDefaultNamespaceFunctionDeclaration();
if (defaultFunctionNamespaceDecl != null && defaultFunctionNamespaceDecl.getURILiteral() != null)
return removeQuotOrApos(defaultFunctionNamespaceDecl.getURILiteral().getText());
else if (isLibraryModule() && XQueryFlavour.MARKLOGIC.equals(getSettings().getFlavour()) && getModuleDeclaration().getURILiteral() != null) {
return removeQuotOrApos(getModuleDeclaration().getURILiteral().getText());
}
return FN.getNamespace();
}

Expand Down Expand Up @@ -378,10 +383,17 @@ public boolean isDefaultFunctionNamespace(String namespace) {

private Map<String, String> calcVariablePrefixToNamespaceMap() {
Map<String, String> namespaceMapping = calcPrefixToNamespaceMap();
namespaceMapping.put(null, XMLConstants.NULL_NS_URI);
namespaceMapping.put(null, getVariableDefaultNamespace());
return namespaceMapping;
}

private String getVariableDefaultNamespace() {
if (isLibraryModule() && XQueryFlavour.MARKLOGIC.equals(getSettings().getFlavour()) && getModuleDeclaration().getURILiteral() != null) {
return removeQuotOrApos(getModuleDeclaration().getURILiteral().getText());
}
return XMLConstants.NULL_NS_URI;
}

public String mapVariablePrefixToNamespace(String prefix) {
return getVariablePrefixToNamespaceMap().get(prefix);
}
Expand All @@ -394,7 +406,7 @@ public Map<String, String> getVariablePrefixToNamespaceMap() {
@Override
public Result<Map<String, String>> compute() {
return CachedValueProvider.Result.create(calcVariablePrefixToNamespaceMap(),
XQueryFile.this);
XQueryFile.this, getSettings());
}
}, false);
}
Expand All @@ -404,4 +416,8 @@ public Result<Map<String, String>> compute() {
public XQueryQueryBody getQueryBody() {
return PsiTreeUtil.findChildOfType(this, XQueryQueryBody.class);
}

private XQuerySettings getSettings() {
return XQuerySettings.getInstance(getProject());
}
}
Expand Up @@ -22,7 +22,6 @@
import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import java.util.Comparator;
import java.util.List;

public class DefaultFileExtensionsPanel extends SettingsPanel {
Expand All @@ -31,17 +30,17 @@ public class DefaultFileExtensionsPanel extends SettingsPanel {
public static final String LIBRARY_MODULE_FILE_EXTENSION = "libraryModuleFileExtension";
private final LabeledComponent<JComboBox> mainModuleFileExtension;
private final LabeledComponent<JComboBox> libraryModuleFileExtension;
private final SortedComboBoxModel<Object> mainModuleFileExtensionModel = comboBoxModel();
private final SortedComboBoxModel<Object> libraryModuleFileExtensionModel = comboBoxModel();
private final SortedComboBoxModel<Object> mainModuleFileExtensionModel = UIUtils.comboBoxModel();
private final SortedComboBoxModel<Object> libraryModuleFileExtensionModel = UIUtils.comboBoxModel();
private final String defaultMainModuleExtension;
private final String defaultLibraryModuleExtension;

public DefaultFileExtensionsPanel(String defaultMainModuleExtension, String defaultLibraryModuleExtension, List<String> allExtensions) {
this.defaultMainModuleExtension = defaultMainModuleExtension;
this.defaultLibraryModuleExtension = defaultLibraryModuleExtension;
setLayout(new MigLayout("ins 0, gap 5, fill, flowy"));
mainModuleFileExtension = comboBox("&Main module", MAIN_MODULE_FILE_EXTENSION, mainModuleFileExtensionModel);
libraryModuleFileExtension = comboBox("&Library module", LIBRARY_MODULE_FILE_EXTENSION, libraryModuleFileExtensionModel);
mainModuleFileExtension = UIUtils.comboBox("&Main module", MAIN_MODULE_FILE_EXTENSION, mainModuleFileExtensionModel);
libraryModuleFileExtension = UIUtils.comboBox("&Library module", LIBRARY_MODULE_FILE_EXTENSION, libraryModuleFileExtensionModel);
add(mainModuleFileExtension);
add(libraryModuleFileExtension);

Expand Down Expand Up @@ -71,23 +70,4 @@ private void populateExtensionsList(SortedComboBoxModel<Object> model, Object de
}
model.setSelectedItem(defaultItem);
}

private SortedComboBoxModel<Object> comboBoxModel() {
return new SortedComboBoxModel<Object>(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return ((String) o1).compareToIgnoreCase((String) o2);
}
});
}

private LabeledComponent<JComboBox> comboBox(String text, String name, SortedComboBoxModel<Object> model) {
LabeledComponent<JComboBox> comboBox = new LabeledComponent<JComboBox>();
comboBox.setText(text);
comboBox.setLabelLocation("West");
comboBox.setComponent(new JComboBox());
comboBox.getComponent().setName(name);
comboBox.getComponent().setModel(model);
return comboBox;
}
}
83 changes: 83 additions & 0 deletions src/main/java/org/intellij/xquery/settings/OtherOptionsPanel.java
@@ -0,0 +1,83 @@
/*
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors
* (see the CONTRIBUTORS file).
*
* 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 org.intellij.xquery.settings;

import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.ui.ListCellRendererWrapper;
import com.intellij.ui.SortedComboBoxModel;
import net.miginfocom.swing.MigLayout;
import org.intellij.xquery.XQueryFlavour;

import javax.swing.*;
import java.util.Comparator;

public class OtherOptionsPanel extends SettingsPanel {

private final LabeledComponent<JComboBox> xqueryFlavour;
private final SortedComboBoxModel<Object> xqueryFlavourModel = comboBoxModel();
private final XQueryFlavour defaultFlavour;

public OtherOptionsPanel(XQueryFlavour defaultFlavour) {
this.defaultFlavour = defaultFlavour;
setLayout(new MigLayout("ins 0, gap 5, fill, flowy"));
xqueryFlavour = UIUtils.comboBox("&XQuery flavour", "xqueryFlavour", xqueryFlavourModel);
xqueryFlavour.getComponent().setRenderer(new XQueryFlavourRenderer());
add(xqueryFlavour);
setBorder(BorderFactory.createTitledBorder("Other options"));
populateFlavourList(defaultFlavour);
}

private void populateFlavourList(XQueryFlavour defaultFlavour) {
for (XQueryFlavour flavour : XQueryFlavour.values()) {
xqueryFlavourModel.add(flavour);
}
xqueryFlavourModel.setSelectedItem(defaultFlavour);
}

@Override
public XQuerySettings updateSettings(XQuerySettings settings) {
settings.setFlavour((XQueryFlavour) xqueryFlavour.getComponent().getSelectedItem());
return settings;
}

@Override
public void updatePanel(XQuerySettings settings) {
if (settings.getFlavour() != null) {
xqueryFlavour.getComponent().setSelectedItem(settings.getFlavour());
} else {
xqueryFlavour.getComponent().setSelectedItem(defaultFlavour);
}
}

private class XQueryFlavourRenderer extends ListCellRendererWrapper<XQueryFlavour> {

@Override
public void customize(JList list, XQueryFlavour value, int index, boolean selected, boolean hasFocus) {
setText(value.getPresentableName());
}
}

private SortedComboBoxModel<Object> comboBoxModel() {
return new SortedComboBoxModel<Object>(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return ((XQueryFlavour) o1).getPresentableName().compareToIgnoreCase(((XQueryFlavour) o2).getPresentableName());
}
});
}
}
45 changes: 45 additions & 0 deletions src/main/java/org/intellij/xquery/settings/UIUtils.java
@@ -0,0 +1,45 @@
/*
* Copyright 2013-2014 Grzegorz Ligas <ligasgr@gmail.com> and other contributors
* (see the CONTRIBUTORS file).
*
* 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 org.intellij.xquery.settings;

import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.ui.SortedComboBoxModel;

import javax.swing.*;
import java.util.Comparator;

public class UIUtils {
static SortedComboBoxModel<Object> comboBoxModel() {
return new SortedComboBoxModel<Object>(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return ((String) o1).compareToIgnoreCase((String) o2);
}
});
}

static LabeledComponent<JComboBox> comboBox(String text, String name, SortedComboBoxModel<Object> model) {
LabeledComponent<JComboBox> comboBox = new LabeledComponent<JComboBox>();
comboBox.setText(text);
comboBox.setLabelLocation("West");
comboBox.setComponent(new JComboBox());
comboBox.getComponent().setName(name);
comboBox.getComponent().setModel(model);
return comboBox;
}
}
31 changes: 27 additions & 4 deletions src/main/java/org/intellij/xquery/settings/XQuerySettings.java
Expand Up @@ -25,8 +25,10 @@
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.components.StorageScheme;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.ModificationTracker;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.intellij.util.xmlb.annotations.Tag;
import org.intellij.xquery.XQueryFlavour;
import org.jetbrains.annotations.Nullable;


Expand All @@ -35,10 +37,12 @@
@Storage(id = "xqueryInDirectory", file = StoragePathMacros.PROJECT_CONFIG_DIR + "/xquery.xml",
scheme = StorageScheme.DIRECTORY_BASED)
})
public class XQuerySettings implements PersistentStateComponent<XQuerySettings> {
public class XQuerySettings implements PersistentStateComponent<XQuerySettings>, ModificationTracker {

private String defaultMainModuleExtension;
private String defaultLibraryModuleExtension;
private XQueryFlavour flavour;
private long modificationCount = 0;

public static XQuerySettings getInstance(Project project) {
return ServiceManager.getService(project, XQuerySettings.class);
Expand All @@ -52,6 +56,9 @@ public XQuerySettings getState() {

@Override
public void loadState(XQuerySettings state) {
if (!this.equals(state)) {
modificationCount++;
}
XmlSerializerUtil.copyBean(state, this);
}

Expand All @@ -73,17 +80,27 @@ public void setDefaultLibraryModuleExtension(String defaultLibraryModuleExtensio
this.defaultLibraryModuleExtension = defaultLibraryModuleExtension;
}

@Tag("flavour")
public XQueryFlavour getFlavour() {
return flavour;
}

public void setFlavour(XQueryFlavour flavour) {
this.flavour = flavour;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

XQuerySettings that = (XQuerySettings) o;
XQuerySettings settings = (XQuerySettings) o;

if (defaultLibraryModuleExtension != null ? !defaultLibraryModuleExtension.equals(that.defaultLibraryModuleExtension) : that.defaultLibraryModuleExtension != null)
if (defaultLibraryModuleExtension != null ? !defaultLibraryModuleExtension.equals(settings.defaultLibraryModuleExtension) : settings.defaultLibraryModuleExtension != null)
return false;
if (defaultMainModuleExtension != null ? !defaultMainModuleExtension.equals(that.defaultMainModuleExtension) : that.defaultMainModuleExtension != null)
if (defaultMainModuleExtension != null ? !defaultMainModuleExtension.equals(settings.defaultMainModuleExtension) : settings.defaultMainModuleExtension != null)
return false;
if (flavour != settings.flavour) return false;

return true;
}
Expand All @@ -92,6 +109,12 @@ public boolean equals(Object o) {
public int hashCode() {
int result = defaultMainModuleExtension != null ? defaultMainModuleExtension.hashCode() : 0;
result = 31 * result + (defaultLibraryModuleExtension != null ? defaultLibraryModuleExtension.hashCode() : 0);
result = 31 * result + (flavour != null ? flavour.hashCode() : 0);
return result;
}

@Override
public long getModificationCount() {
return modificationCount;
}
}

0 comments on commit ea69192

Please sign in to comment.