Refactoring: extract detecting platform functions to separate class

This commit is contained in:
Nikolay Krasko
2013-04-01 12:31:58 +04:00
parent a13d61e618
commit 307d9b1661
4 changed files with 117 additions and 32 deletions
@@ -16,48 +16,17 @@
package org.jetbrains.jet.plugin.project;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.analyzer.AnalyzerFacade;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.plugin.framework.KotlinFrameworkDetector;
public final class AnalyzerFacadeProvider {
private final static Logger LOG = Logger.getInstance(AnalyzerFacade.class);
private AnalyzerFacadeProvider() {
}
@NotNull
public static AnalyzerFacade getAnalyzerFacadeForFile(@NotNull JetFile file) {
VirtualFile virtualFile = file.getOriginalFile().getVirtualFile();
if (virtualFile == null) {
return getDefaultAnalyzerFacade();
}
Module moduleForFile = ProjectFileIndex.SERVICE.getInstance(file.getProject()).getModuleForFile(virtualFile);
if (moduleForFile == null) {
return getDefaultAnalyzerFacade();
}
return getAnalyzerFacadeForModule(moduleForFile);
}
@NotNull
private static AnalyzerFacade getDefaultAnalyzerFacade() {
//TODO: should deal with situations when we can't determine whether to use java or js backend more carefully
LOG.info("Using default analyzer facade");
return AnalyzerFacadeForJVM.INSTANCE;
}
@NotNull
private static AnalyzerFacade getAnalyzerFacadeForModule(@NotNull Module module) {
if (KotlinFrameworkDetector.isJsKotlinModule(module)) {
return JSAnalyzerFacadeForIDEA.INSTANCE;
}
return AnalyzerFacadeForJVM.INSTANCE;
return TargetPlatformDetector.getPlatform(file) == TargetPlatform.JVM ? AnalyzerFacadeForJVM.INSTANCE : JSAnalyzerFacadeForIDEA.INSTANCE;
}
}
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2013 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.project;
public interface TargetPlatform {
TargetPlatform JVM = new TargetPlatformImpl("JVM");
TargetPlatform JS = new TargetPlatformImpl("JS");
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2013 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.project;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.analyzer.AnalyzerFacade;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.framework.KotlinFrameworkDetector;
public class TargetPlatformDetector {
public static final TargetPlatformDetector INSTANCE = new TargetPlatformDetector();
private static final Logger LOG = Logger.getInstance(AnalyzerFacade.class);
private TargetPlatformDetector() {
}
@NotNull
public static TargetPlatform getPlatform(@NotNull JetFile file) {
VirtualFile virtualFile = file.getOriginalFile().getVirtualFile();
if (virtualFile == null) {
return getDefaultPlatform();
}
Module moduleForFile = ProjectFileIndex.SERVICE.getInstance(file.getProject()).getModuleForFile(virtualFile);
if (moduleForFile == null) {
return getDefaultPlatform();
}
return getPlatform(moduleForFile);
}
@NotNull
public static TargetPlatform getPlatform(@NotNull Module module) {
if (KotlinFrameworkDetector.isJsKotlinModule(module)) {
return TargetPlatform.JS;
}
return TargetPlatform.JVM;
}
@NotNull
private static TargetPlatform getDefaultPlatform() {
LOG.info("Using default platform");
return TargetPlatform.JVM;
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2013 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.project;
import org.jetbrains.annotations.NotNull;
public class TargetPlatformImpl implements TargetPlatform {
@NotNull private final String platformName;
public TargetPlatformImpl(@NotNull String platformName) {
this.platformName = platformName;
}
@Override
public String toString() {
return platformName;
}
}