forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatMarkupViewer.java
More file actions
94 lines (80 loc) · 3.46 KB
/
Copy pathChatMarkupViewer.java
File metadata and controls
94 lines (80 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.chat;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.MultipleHyperlinkPresenter;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage;
import org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder;
import org.eclipse.mylyn.wikitext.parser.css.CssParser;
import org.eclipse.mylyn.wikitext.ui.viewer.MarkupViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import com.microsoft.copilot.eclipse.core.CopilotCore;
import com.microsoft.copilot.eclipse.ui.CopilotUi;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
class ChatMarkupViewer extends MarkupViewer {
public ChatMarkupViewer(Composite parent, int styles) {
super(parent, null, styles);
this.setMarkupLanguage(new MarkdownLanguage());
this.setDisplayImages(false);
IHyperlinkDetector[] hyperlinkDetectors = { new FileAnnotationHyperlinkDetector() };
this.setHyperlinkDetectors(hyperlinkDetectors, SWT.NONE);
MultipleHyperlinkPresenter hyperlinkPresenter = new MultipleHyperlinkPresenter((RGB) null);
this.setHyperlinkPresenter(hyperlinkPresenter);
// Register for chat font updates via centralized service
var chatServiceManager = CopilotUi.getPlugin().getChatServiceManager();
if (chatServiceManager != null) {
chatServiceManager.getChatFontService().registerControl(getTextWidget());
}
loadStylesheet();
}
private void loadStylesheet() {
if (UiUtils.isDarkTheme()) {
URL cssUrl = CopilotUi.getPlugin().getBundle().getEntry("css/markup-viewer-dark.css");
if (cssUrl != null) {
try (Reader reader = new InputStreamReader(cssUrl.openStream(), StandardCharsets.UTF_8)) {
this.setStylesheet(new CssParser().parse(reader));
} catch (IOException e) {
CopilotCore.LOGGER.error("Failed to load dark mode stylesheet for markup viewer", e);
}
}
}
}
// MarkupViewer will write errors when failed to parse the markup, which will send the error to the Copilot.
// so overwrite the setMarkup method to avoid sending the error.
@Override
public void setMarkup(String source) {
try {
String htmlText = this.computeHtml(source);
setHtml(htmlText);
// reset text presentation to update the style, otherwise the style won't be updated
this.setTextPresentation(getTextPresentation());
} catch (Throwable t) {
if (getTextPresentation() != null) {
getTextPresentation().clear();
}
setDocumentNoMarkup(new Document(source), new AnnotationModel());
// TODO: Whether we should track the parse exception?
}
}
// computeHtml(String) is a private method in MarkupViewer, so copy it here.
private String computeHtml(String markupContent) {
StringWriter out = new StringWriter();
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out);
builder.setFilterEntityReferences(true);
getParser().setBuilder(builder);
getParser().parse(markupContent);
getParser().setBuilder(null);
String htmlText = out.toString();
return htmlText;
}
}