no preview available message

This commit is contained in:
Ekaterina Tuzova
2018-06-14 15:01:44 +03:00
parent 0f8e7428ed
commit 4baf285e7d
4 changed files with 39 additions and 48 deletions

View File

@@ -1,43 +0,0 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.rest;
import com.google.common.collect.Lists;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.ProcessOutput;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.sdk.PySdkUtil;
import com.jetbrains.python.sdk.PythonSdkType;
import com.jetbrains.rest.editor.RestPreviewProvider;
import org.jetbrains.annotations.Nullable;
import static com.jetbrains.python.PythonHelper.REST_RUNNER;
public class RestPythonPreviewProvider extends RestPreviewProvider {
@Nullable
public String toHtml(String text, VirtualFile virtualFile, Project project) {
Module module = ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(virtualFile);
if (module == null) return null;
final Sdk sdk = PythonSdkType.findPythonSdk(module);
if (sdk == null) {
return null;
}
final String sdkHomePath = sdk.getHomePath();
if (sdkHomePath == null) {
return null;
}
final GeneralCommandLine commandLine = REST_RUNNER.newCommandLine(sdkHomePath, Lists.newArrayList("rst2html"));
final ProcessOutput output = PySdkUtil.getProcessOutput(commandLine, virtualFile.getParent().getPath(), null, 5000,
text.getBytes(CharsetToolkit.UTF8_CHARSET), true);
if (output.isCancelled() || output.isTimeout()) return null;
return output.getStdout();
}
}

View File

@@ -0,0 +1,27 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.rest
import com.google.common.collect.Lists
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.CharsetToolkit
import com.intellij.openapi.vfs.VirtualFile
import com.jetbrains.python.PythonHelper.REST_RUNNER
import com.jetbrains.python.sdk.PySdkUtil
import com.jetbrains.python.sdk.PythonSdkType
import com.jetbrains.rest.editor.RestPreviewProvider
class RestPythonPreviewProvider : RestPreviewProvider() {
override fun toHtml(text: String, virtualFile: VirtualFile, project: Project): Pair<String, String>? {
val module = ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(virtualFile) ?: return null
val sdk = PythonSdkType.findPythonSdk(module) ?: return null
val sdkHomePath = sdk.homePath ?: return null
val commandLine = REST_RUNNER.newCommandLine(sdkHomePath, Lists.newArrayList("rst2html"))
val output = PySdkUtil.getProcessOutput(commandLine, virtualFile.parent.path, null, 5000,
text.toByteArray(CharsetToolkit.UTF8_CHARSET), true)
return if (output.isCancelled || output.isTimeout) null else Pair(output.stdout, output.stderr)
}
}

View File

@@ -17,6 +17,7 @@ import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.javafx.JavaFxHtmlPanel;
import com.intellij.util.Alarm;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.owasp.html.HtmlPolicyBuilder;
@@ -28,6 +29,7 @@ import java.beans.PropertyChangeListener;
public class RestPreviewFileEditor extends UserDataHolderBase implements FileEditor {
private final static long PARSING_CALL_TIMEOUT_MS = 50L;
private final static String NO_PREVIEW = "<h2>No preview available.</h2><br/><br/><h3>Error output:</h3>";
private final static long RENDERING_DELAY_MS = 20L;
@@ -140,9 +142,12 @@ public class RestPreviewFileEditor extends UserDataHolderBase implements FileEdi
return;
}
final String html = RestPreviewProvider.getProviders()[0].toHtml(myDocument.getText(), myFile, myProject);
if (html == null) {
return;
final Pair<String, String> htmlAndError = RestPreviewProvider.getProviders()[0].toHtml(myDocument.getText(), myFile, myProject);
if (htmlAndError == null) return;
String html = htmlAndError.getFirst();
if (html.isEmpty()) {
html = NO_PREVIEW + htmlAndError.getSecond();
}
// EA-75860: The lines to the top may be processed slowly; Since we're in pooled thread, we can be disposed already.
@@ -154,8 +159,9 @@ public class RestPreviewFileEditor extends UserDataHolderBase implements FileEdi
if (myLastRequest != null) {
mySwingAlarm.cancelRequest(myLastRequest);
}
String finalHtml = html;
myLastRequest = () -> {
final String currentHtml = "<html>" + SANITIZER_VALUE.getValue().sanitize(html) + "</html>";
final String currentHtml = "<html>" + SANITIZER_VALUE.getValue().sanitize(finalHtml) + "</html>";
if (!currentHtml.equals(myLastRenderedHtml)) {
myLastRenderedHtml = currentHtml;
myPanel.setHtml(myLastRenderedHtml);

View File

@@ -4,6 +4,7 @@ package com.jetbrains.rest.editor;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -12,7 +13,7 @@ public abstract class RestPreviewProvider {
ExtensionPointName.create("restructured.text.html.preview.provider");
@Nullable
public abstract String toHtml(String text, VirtualFile virtualFile, Project project);
public abstract Pair<String, String> toHtml(String text, VirtualFile virtualFile, Project project);
@NotNull
public static RestPreviewProvider[] getProviders() {