Skip to content

Commit

Permalink
First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
halirutan committed Dec 22, 2017
1 parent 3da9d82 commit 396401e
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 67 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -78,7 +78,7 @@ task wrapper(type: Wrapper) {
version '3.0pre5'

intellij {
version = '2017.3'
version = '2017.3.1'
downloadSources = true
pluginName = 'Mathematica-IntelliJ-Plugin'
updateSinceUntilBuild = false
Expand Down
19 changes: 19 additions & 0 deletions config/mit.txt
@@ -0,0 +1,19 @@
Copyright (c) $today.year Patrick Scheibe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 2 additions & 4 deletions resources/META-INF/plugin.xml
Expand Up @@ -39,10 +39,8 @@

<sdkType id="Mathematica Sdk" implementation="de.halirutan.mathematica.sdk.MathematicaSdkType"/>
<moduleType id="MATHEMATICA_MODULE" implementationClass="de.halirutan.mathematica.module.MathematicaModuleType"/>
<!--<moduleBuilder builderClass="de.halirutan.mathematica.module.MathematicaEmptyModule"/>-->
<!--<moduleBuilder builderClass="de.halirutan.mathematica.module.MathematicaBasicModule"/>-->
<!--<moduleBuilder builderClass="de.halirutan.mathematica.module.MathematicaApplicationModule"/>-->
<moduleConfigurationEditorProvider implementation="de.halirutan.mathematica.module.MathematicaModuleConfigurationEditor"/>
<moduleExtension implementation="de.halirutan.mathematica.module.MathematicaLanguageLevelModuleExtensionImpl"/>
<moduleConfigurationEditorProvider implementation="de.halirutan.mathematica.module.ui.MathematicaModuleConfigurationEditor"/>
<projectTemplatesFactory implementation="de.halirutan.mathematica.module.MathematicaProjectTemplatesFactory"/>

<lang.syntaxHighlighterFactory language="Mathematica" implementationClass="de.halirutan.mathematica.codeinsight.highlighting.MathematicaSyntaxHighlighterFactory"/>
Expand Down
Expand Up @@ -31,12 +31,12 @@ import de.halirutan.mathematica.codeinsight.inspections.symbol.UnresolvedSymbolI
* @author patrick (7/8/14)
*/
class MathematicaInspectionProvider : InspectionToolProvider {
override fun getInspectionClasses(): Array<Class<*>> {
return arrayOf(
UnresolvedSymbolInspection::class.java,
ConsistentCompoundExpressionInFile::class.java,
ImplicitTimesThroughLinebreak::class.java,
UnsupportedVersion::class.java
)
}
override fun getInspectionClasses(): Array<Class<*>> {
return arrayOf(
UnresolvedSymbolInspection::class.java,
ConsistentCompoundExpressionInFile::class.java,
ImplicitTimesThroughLinebreak::class.java,
UnsupportedVersion::class.java
)
}
}
Expand Up @@ -26,13 +26,20 @@
import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleFileIndex;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.VerticalFlowLayout;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import de.halirutan.mathematica.codeinsight.completion.util.SymbolVersionProvider;
import de.halirutan.mathematica.codeinsight.inspections.AbstractInspection;
import de.halirutan.mathematica.file.MathematicaFileType;
Expand All @@ -41,6 +48,8 @@
import de.halirutan.mathematica.lang.psi.api.Symbol;
import de.halirutan.mathematica.lang.psi.api.lists.Association;
import de.halirutan.mathematica.lang.psi.impl.LightBuiltInSymbol;
import de.halirutan.mathematica.module.MathematicaLanguageLevelModuleExtensionImpl;
import de.halirutan.mathematica.module.MathematicaModuleType;
import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;
import de.halirutan.mathematica.sdk.MathematicaSdkType;
import org.jetbrains.annotations.Nls;
Expand All @@ -64,6 +73,7 @@ public class UnsupportedVersion extends AbstractInspection {

@SuppressWarnings({"InstanceVariableNamingConvention", "WeakerAccess"})
public boolean useSDKLanguageLevelOrHighest = true;
public boolean useModuleLanguageLevelOrHighest = true;

/**
* Sets the correct text for the info label in the inspection settings page
Expand All @@ -82,6 +92,7 @@ private void setLabelTextToVersion(JLabel label) {
@Override
public JComponent createOptionsPanel() {
final JPanel mainPanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP));
final JCheckBox useModuleCheckbox = new JCheckBox("Language Level of Module has priority");
final JCheckBox useSDKCheckbox = new JCheckBox("Use Project SDK Language Level");
final JLabel infoLabel = new JLabel();
//noinspection Since15
Expand All @@ -102,6 +113,11 @@ public JComponent createOptionsPanel() {
}
});

useModuleCheckbox.setSelected(useModuleLanguageLevelOrHighest);
useModuleCheckbox.addActionListener(e ->
useModuleLanguageLevelOrHighest = useModuleCheckbox.isSelected()
);

useSDKCheckbox.setSelected(useSDKLanguageLevelOrHighest);
useSDKCheckbox.addActionListener(e -> {
useSDKLanguageLevelOrHighest = useSDKCheckbox.isSelected();
Expand All @@ -114,8 +130,7 @@ public JComponent createOptionsPanel() {

setLabelTextToVersion(infoLabel);
versionComboBox.setVisible(!useSDKLanguageLevelOrHighest);

mainPanel.add(infoLabel);
mainPanel.add(useModuleCheckbox);
mainPanel.add(useSDKCheckbox);
mainPanel.add(versionComboBox);

Expand Down Expand Up @@ -152,6 +167,27 @@ public HighlightDisplayLevel getDefaultLevel() {
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
if (session.getFile().getFileType() instanceof MathematicaFileType) {

// TODO: There must be a simpler way to find the module for a source file
if (useModuleLanguageLevelOrHighest) {
final PsiFile file = session.getFile();
final ModuleManager instance = ModuleManager.getInstance(file.getProject());
for (Module module : instance.getModules()) {
if (ModuleType.is(module, MathematicaModuleType.getInstance())) {
final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
final ModuleFileIndex fileIndex = moduleRootManager.getFileIndex();
final VirtualFile virtualFile = file.getVirtualFile();
if (fileIndex.isInContent(virtualFile)) {
final MathematicaLanguageLevelModuleExtensionImpl languageLevelModuleExtension =
moduleRootManager.getModuleExtension(MathematicaLanguageLevelModuleExtensionImpl.class);
if (languageLevelModuleExtension.getMathematicaLanguageLevel() != null) {
return new WrongVersionVisitor(holder, languageLevelModuleExtension.getMathematicaLanguageLevel());
}
}
}
}
}

if (useSDKLanguageLevelOrHighest) {
final ProjectRootManager manager = ProjectRootManager.getInstance(holder.getProject());
final Sdk projectSdk = manager.getProjectSdk();
Expand All @@ -170,12 +206,12 @@ public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, fina
private static class WrongVersionVisitor extends MathematicaVisitor {

private final HashMap<String, Double> mySymbolVersions = SymbolVersionProvider.getSymbolNames();
private MathematicaLanguageLevel myLanguageLevel;
private final ProblemsHolder myHolder;
private MathematicaLanguageLevel myLanguageLevel;

WrongVersionVisitor(final ProblemsHolder holder, final MathematicaLanguageLevel usedLanguageVersion) {
this.myHolder = holder;
myLanguageLevel = usedLanguageVersion;
myLanguageLevel = usedLanguageVersion;
}

private void registerProblem(final PsiElement element, final String message) {
Expand All @@ -188,11 +224,13 @@ private void registerProblem(final PsiElement element, final String message) {
@Override
public void visitFunctionCall(FunctionCall functionCall) {
final PsiElement head = functionCall.getHead();
if ("Association".equals(head.getText()) && myLanguageLevel.getVersionNumber() < 10 ) {
registerProblem(functionCall, message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
if ("Association".equals(head.getText()) && myLanguageLevel.getVersionNumber() < 10) {
registerProblem(functionCall,
message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
}

if (functionCall.hasHead("With") && myLanguageLevel.getVersionNumber() < 10.3 && functionCall.getArguments().size() > 3) {
if (functionCall.hasHead("With") && myLanguageLevel.getVersionNumber() < 10.3 &&
functionCall.getArguments().size() > 3) {
registerProblem(functionCall, message("bugs.unsupported.version.with", myLanguageLevel.getPresentableText()));

}
Expand All @@ -201,7 +239,8 @@ public void visitFunctionCall(FunctionCall functionCall) {
@Override
public void visitAssociation(Association association) {
if (myLanguageLevel.getVersionNumber() < 10) {
registerProblem(association, message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
registerProblem(association,
message("bugs.unsupported.version.association", myLanguageLevel.getPresentableText()));
}
}

Expand All @@ -214,12 +253,14 @@ public void visitSymbol(Symbol symbol) {

final PsiElement resolve = symbol.resolve();
if (resolve instanceof LightBuiltInSymbol) {
String nameWithContext = symbol.getMathematicaContext().equals("") ? "System`" + symbol.getSymbolName() : symbol.getFullSymbolName();
String nameWithContext =
symbol.getMathematicaContext().equals("") ? "System`" + symbol.getSymbolName() : symbol.getFullSymbolName();

if (mySymbolVersions.containsKey(nameWithContext)) {
double version = mySymbolVersions.get(nameWithContext);
if (version > myLanguageLevel.getVersionNumber()) {
registerProblem(symbol, "Mathematica " + version + " required. You are using " + myLanguageLevel.getPresentableText());
registerProblem(symbol,
"Mathematica " + version + " required. You are using " + myLanguageLevel.getPresentableText());
}
}
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ui.configuration.CommonContentEntriesEditor;
import com.intellij.openapi.roots.ui.configuration.ModuleConfigurationState;
import de.halirutan.mathematica.module.ui.MathematicaLanguageLevelCombo;
import org.jetbrains.jps.model.java.JavaResourceRootType;
import org.jetbrains.jps.model.java.JavaSourceRootType;

Expand Down

This file was deleted.

@@ -0,0 +1,16 @@
package de.halirutan.mathematica.module;

import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;

import javax.annotation.Nullable;

/**
* @author patrick (21.12.17).
*/
public interface MathematicaLanguageLevelModuleExtension {
@Nullable
MathematicaLanguageLevel getMathematicaLanguageLevel();

void setMathematicaLanguageLevel(MathematicaLanguageLevel languageLevel);

}
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2017 Patrick Scheibe
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package de.halirutan.mathematica.module;

import com.intellij.openapi.components.BaseState;
import com.intellij.openapi.components.PersistentStateComponentWithModificationTracker;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ModuleExtension;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.util.xmlb.annotations.Property;
import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;
import org.jetbrains.annotations.Nullable;

/**
* @author patrick (13.12.17).
*/
public class MathematicaLanguageLevelModuleExtensionImpl extends ModuleExtension implements MathematicaLanguageLevelModuleExtension,
PersistentStateComponentWithModificationTracker<MathematicaLanguageLevelModuleExtensionImpl.State> {

private static final Logger LOG = Logger.getInstance(MathematicaLanguageLevelModuleExtensionImpl.class);

private boolean myWritable;
private Module myModule;
private MathematicaLanguageLevelModuleExtensionImpl mySource;
private State myState;

public MathematicaLanguageLevelModuleExtensionImpl(Module module) {
myWritable = false;
myModule = module;
mySource = null;
myState = new State();
}

public MathematicaLanguageLevelModuleExtensionImpl(MathematicaLanguageLevelModuleExtensionImpl source, boolean writable) {
this.myWritable = writable;
myModule = source.myModule;
mySource = source;
myState = new State();
myState.versionNumber = source.myState.versionNumber;
}

@Nullable
public static MathematicaLanguageLevelModuleExtensionImpl getInstance(final Module module) {
return ModuleRootManager.getInstance(module).getModuleExtension(MathematicaLanguageLevelModuleExtensionImpl.class);
}

@Override
public long getStateModificationCount() {
return myState.getModificationCount();
}

@Override
public ModuleExtension getModifiableModel(boolean writable) {
return new MathematicaLanguageLevelModuleExtensionImpl(this, writable);
}

@Override
public void commit() {
if (isChanged()) {
mySource.myState = myState;
}
}

@Override
public boolean isChanged() {
return mySource != null && mySource.myState.versionNumber != myState.versionNumber;
}

@Nullable
@Override
public State getState() {
return myState;
}

@Override
public void loadState(State state) {
myState = state;
}

@Override
public void dispose() {
myModule = null;
myState = null;
}

@Nullable
@Override
public MathematicaLanguageLevel getMathematicaLanguageLevel() {
return MathematicaLanguageLevel.fromDouble(myState.versionNumber);
}

@Override
public void setMathematicaLanguageLevel(MathematicaLanguageLevel languageLevel) {
LOG.assertTrue(myWritable, "Writable model can be retrieved from writable ModifiableRootModel");
myState.versionNumber = languageLevel.getVersionNumber();
}

static class State extends BaseState {
@Property
double versionNumber;
}
}
Expand Up @@ -24,6 +24,7 @@
import com.intellij.ide.util.projectWizard.SdkSettingsStep;
import com.intellij.ide.util.projectWizard.SettingsStep;
import com.intellij.openapi.projectRoots.Sdk;
import de.halirutan.mathematica.module.ui.MathematicaLanguageLevelComboBox;
import de.halirutan.mathematica.sdk.MathematicaLanguageLevel;
import de.halirutan.mathematica.sdk.MathematicaSdkType;
import org.jetbrains.annotations.NotNull;
Expand Down

0 comments on commit 396401e

Please sign in to comment.