Add a draft for K2JS configuration in plugin.

Minor change in QualifiedExpressionTranslator.
This commit is contained in:
Pavel V. Talanov
2012-03-20 19:32:34 +04:00
parent e4ebdb9f18
commit c4f59ff7de
7 changed files with 312 additions and 8 deletions
@@ -25,7 +25,6 @@ import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
@@ -52,7 +51,7 @@ import static org.jetbrains.jet.plugin.actions.JavaToKotlinActionUtil.allVirtual
*/
public final class TranslateToJsAction extends AnAction {
private static void notifyFailure(@NotNull Throwable exception) {
public static void notifyFailure(@NotNull Throwable exception) {
Notifications.Bus.notify(new Notification("JsTranslator", "Translation failed.",
"Exception: " + exception.getMessage(),
NotificationType.ERROR));
@@ -70,7 +69,8 @@ public final class TranslateToJsAction extends AnAction {
public void run() {
try {
performAction(event);
} catch (Throwable e) {
}
catch (Throwable e) {
e.printStackTrace();
notifyFailure(e);
}
@@ -82,6 +82,10 @@ public final class TranslateToJsAction extends AnAction {
private static void performAction(@NotNull AnActionEvent event) throws Exception {
final Project project = PlatformDataKeys.PROJECT.getData(event.getDataContext());
assert project != null;
doPerform(project);
}
public static void doPerform(@NotNull Project project) throws Exception {
Set<VirtualFile> allVirtualFiles = getAllProjectVirtualFiles(project);
List<JetFile> kotlinFiles = getJetFiles(allVirtualFiles, project);
String outputPath = getOutputPath(JetMainDetector.getFileWithMain(kotlinFiles));
@@ -111,7 +115,7 @@ public final class TranslateToJsAction extends AnAction {
for (VirtualFile virtualFile : virtualFiles) {
PsiFile psiFile = psiManager.findFile(virtualFile);
if (psiFile instanceof JetFile) {
kotlinFiles.add((JetFile) psiFile);
kotlinFiles.add((JetFile)psiFile);
}
}
return kotlinFiles;
@@ -129,13 +133,13 @@ public final class TranslateToJsAction extends AnAction {
//TODO: make platform independent
String pathToDir = originalFilePath.substring(0, originalFilePath.lastIndexOf("/") + 1);
String generatedFileName = ((JetFile) psiFile).getNamespaceHeader().getName() + ".js";
String generatedFileName = ((JetFile)psiFile).getNamespaceHeader().getName() + ".js";
return pathToDir + generatedFileName;
}
@Override
public void update(AnActionEvent e) {
Editor editor = e.getData(PlatformDataKeys.EDITOR);
e.getPresentation().setEnabled(editor != null);
// Editor editor = e.getData(PlatformDataKeys.EDITOR);
// e.getPresentation().setEnabled(editor != null);
}
}
@@ -0,0 +1,86 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* 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.jetbrains.jet.plugin.k2jsrun;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.GenericProgramRunner;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.ide.browsers.BrowsersConfiguration;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFileManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.plugin.actions.TranslateToJsAction;
/**
* @author Pavel Talanov
*/
public final class K2JSBrowserProgramRunner extends GenericProgramRunner {
@Override
protected RunContentDescriptor doExecute(Project project,
Executor executor,
RunProfileState state,
RunContentDescriptor contentToReuse,
ExecutionEnvironment env) throws ExecutionException {
if (project == null) {
return null;
}
try {
K2JSConfigurationSettings configurationSettings = getSettings(state);
try {
TranslateToJsAction.doPerform(project);
}
catch (Throwable e) {
TranslateToJsAction.notifyFailure(e);
return null;
}
openBrowser(configurationSettings);
}
catch (Exception e) {
throw new ExecutionException(e);
}
return null;
}
private static void openBrowser(@NotNull K2JSConfigurationSettings configurationSettings) {
String filePath = configurationSettings.getFilePath();
String url = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, filePath);
BrowsersConfiguration.launchBrowser(BrowsersConfiguration.BrowserFamily.FIREFOX, url);
}
@NotNull
private static K2JSConfigurationSettings getSettings(@NotNull RunProfileState state) {
RunProfile profile = state.getRunnerSettings().getRunProfile();
assert profile instanceof K2JSRunConfiguration;
return ((K2JSRunConfiguration)profile).settings();
}
@NotNull
@Override
public String getRunnerId() {
return "K2JSBrowserRunner";
}
@Override
public boolean canRun(@NotNull String executorId, @NotNull RunProfile profile) {
return (profile instanceof K2JSRunConfiguration);
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* 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.jetbrains.jet.plugin.k2jsrun;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel Talanov
*/
public class K2JSConfigurationSettings {
@NotNull
private String filePath = "";
@NotNull
public String getFilePath() {
return filePath;
}
public void setFilePath(@NotNull String filePath) {
this.filePath = filePath;
}
}
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.jet.plugin.k2jsrun.K2JSRunConfigurationEditor">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="aad78" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5ba6" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="chooseFile">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="cf71a" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="HTML file"/>
</properties>
</component>
<component id="f4230" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="a2cc0"/>
<text value="Browser"/>
</properties>
</component>
<component id="a2cc0" class="javax.swing.JComboBox">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
<vspacer id="e9321">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
<buttonGroups>
<group name="group">
<member id="5e12a"/>
<member id="ae2bc"/>
</group>
</buttonGroups>
</form>
@@ -0,0 +1,69 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* 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.jetbrains.jet.plugin.k2jsrun;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
/**
* @author Pavel Talanov
*/
public final class K2JSRunConfigurationEditor extends SettingsEditor<K2JSRunConfiguration> {
private JPanel mainPanel;
private TextFieldWithBrowseButton chooseFile;
@NotNull
private final Project project;
public K2JSRunConfigurationEditor(@NotNull Project project) {
this.project = project;
}
@Override
protected void resetEditorFrom(K2JSRunConfiguration configuration) {
chooseFile.setText(configuration.settings().getFilePath());
}
@Override
protected void applyEditorTo(@NotNull K2JSRunConfiguration configuration) throws ConfigurationException {
configuration.settings().setFilePath(FileUtil.toSystemIndependentName(chooseFile.getText()));
}
@NotNull
@Override
protected JComponent createEditor() {
FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor(StdFileTypes.HTML);
fileChooserDescriptor.setRoots(ProjectRootManager.getInstance(project).getContentRootUrls());
chooseFile.addBrowseFolderListener("Choose file", "Yeah!", project, fileChooserDescriptor);
return mainPanel;
}
@Override
protected void disposeEditor() {
// do nothing
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* 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.jetbrains.jet.plugin.k2jsrun;
import com.intellij.execution.configurations.*;
import com.intellij.openapi.project.Project;
import org.jetbrains.jet.plugin.JetFileType;
/**
* @author Pavel Talanov
*/
public final class K2JSRunConfigurationType extends ConfigurationTypeBase {
public static K2JSRunConfigurationType getInstance() {
return ConfigurationTypeUtil.findConfigurationType(K2JSRunConfigurationType.class);
}
public K2JSRunConfigurationType() {
super("K2JSConfigurationType", "K2JS", "Kotlin to Javascript", JetFileType.INSTANCE.getIcon());
addFactory(new K2JSConfigurationFactory());
}
private class K2JSConfigurationFactory extends ConfigurationFactory {
protected K2JSConfigurationFactory() {
super(K2JSRunConfigurationType.this);
}
@Override
public RunConfiguration createTemplateConfiguration(Project project) {
return new K2JSRunConfiguration("", new RunConfigurationModule(project), this);
}
}
}
@@ -40,7 +40,6 @@ public final class QualifiedExpressionTranslator {
@NotNull
public static AccessTranslator getAccessTranslator(@NotNull JetQualifiedExpression expression,
@NotNull TranslationContext context) {
JsExpression receiver = translateReceiver(expression, context);
PropertyAccessTranslator result =
PropertyAccessTranslator.newInstance(getNotNullSimpleNameSelector(expression), receiver,