-
Notifications
You must be signed in to change notification settings - Fork 8
feat: style description panel #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ShawkyZ
merged 9 commits into
feat/IDE-711_create_a_new_snyk_view
from
feat/style-description-panel
Nov 22, 2024
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f50b944
wip: html provider
ShawkyZ 0a4d042
fix: populate tree after snyk.Scan success
ShawkyZ 3f2426c
refactor: use css from LS
ShawkyZ 1affa1b
wip: html provider
ShawkyZ f54fd1c
fix: merge conflicts
ShawkyZ 88f03c8
fix: added HtmlProviderFactory tests
ShawkyZ d75a346
fix: use comperator for sorting issues
ShawkyZ 9082725
refactor: create BrowserHandler
ShawkyZ d43b456
fix: cache theme instance
ShawkyZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
plugin/src/main/java/io/snyk/eclipse/plugin/html/BaseHtmlProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.snyk.eclipse.plugin.html; | ||
|
||
import java.util.Random; | ||
|
||
import org.eclipse.jface.resource.ColorRegistry; | ||
import org.eclipse.swt.graphics.Color; | ||
import org.eclipse.swt.graphics.RGB; | ||
import org.eclipse.ui.PlatformUI; | ||
import org.eclipse.ui.themes.ITheme; | ||
import org.eclipse.ui.themes.IThemeManager; | ||
|
||
public class BaseHtmlProvider { | ||
public String getCss() { | ||
return ""; | ||
} | ||
|
||
public String getJs() { | ||
return ""; | ||
} | ||
|
||
public String getInitScript() { | ||
return ""; | ||
} | ||
|
||
public String getNonce() { | ||
String allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
Random random = new Random(); | ||
StringBuilder nonceBuilder = new StringBuilder(32); | ||
for (int i = 0; i < 32; i++) { | ||
nonceBuilder.append(allowedChars.charAt(random.nextInt(allowedChars.length()))); | ||
} | ||
return nonceBuilder.toString(); | ||
} | ||
|
||
public String replaceCssVariables(String html) { | ||
// Build the CSS with the nonce | ||
String nonce = getNonce(); | ||
String css = "<style nonce=\"" + nonce + "\">" + getCss() + "</style>"; | ||
html = html.replace("${ideStyle}", css); | ||
html = html.replace("<style nonce=\"ideNonce\" data-ide-style></style>", css); | ||
html = html.replace("var(--default-font)", " ui-sans-serif, \"SF Pro Text\", \"Segoe UI\", \"Ubuntu\", Tahoma, Geneva, Verdana, sans-serif;"); | ||
|
||
|
||
// Replace CSS variables with actual color values | ||
html = html.replace("var(--text-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR", "#000000")); | ||
html = html.replace("var(--background-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_START", "#FFFFFF")); | ||
html = html.replace("var(--border-color)", getColorAsHex( "org.eclipse.ui.workbench.ACTIVE_TAB_BORDER_COLOR", "#CCCCCC")); | ||
html = html.replace("var(--link-color)", getColorAsHex("org.eclipse.ui.workbench.HYPERLINK_COLOR", "#0000FF")); | ||
html = html.replace("var(--horizontal-border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_HIGHLIGHT_BORDER_COLOR", "#CCCCCC")); | ||
html = html.replace("var(--code-background-color)", getColorAsHex("org.eclipse.ui.workbench.CODE_BACKGROUND_COLOR", "#F0F0F0")); | ||
|
||
// Update the HTML head | ||
String ideHeaders = """ | ||
<head> | ||
<meta http-equiv='Content-Type' content='text/html; charset=unicode' /> | ||
<meta http-equiv='X-UA-Compatible' content='IE=edge' /> | ||
"""; | ||
html = html.replace("<head>", ideHeaders); | ||
html = html.replace("${headerEnd}", ""); | ||
|
||
// Replace nonce placeholders | ||
html = html.replace("${nonce}", nonce); | ||
html = html.replace("ideNonce", nonce); | ||
html = html.replace("${ideScript}", ""); | ||
|
||
return html; | ||
} | ||
|
||
public String getColorAsHex(String colorKey, String defaultColor) { | ||
ColorRegistry colorRegistry = getColorRegistry(); | ||
Color color = colorRegistry.get(colorKey); | ||
if (color == null) { | ||
return defaultColor; | ||
} else { | ||
RGB rgb = color.getRGB(); | ||
return String.format("#%02x%02x%02x", rgb.red, rgb.green, rgb.blue); | ||
} | ||
} | ||
|
||
private ColorRegistry getColorRegistry() { | ||
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); | ||
ITheme currentTheme = themeManager.getCurrentTheme(); | ||
return currentTheme.getColorRegistry(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
plugin/src/main/java/io/snyk/eclipse/plugin/html/CodeHtmlProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.snyk.eclipse.plugin.html; | ||
|
||
import org.eclipse.ui.PlatformUI; | ||
import org.eclipse.ui.themes.ITheme; | ||
import org.eclipse.ui.themes.IThemeManager; | ||
|
||
public class CodeHtmlProvider extends BaseHtmlProvider { | ||
private static CodeHtmlProvider instance = new CodeHtmlProvider(); | ||
|
||
public static CodeHtmlProvider getInstance() { | ||
if (instance == null) { | ||
synchronized (CodeHtmlProvider.class) { | ||
if (instance == null) { | ||
instance = new CodeHtmlProvider(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
@Override | ||
public String getInitScript() { | ||
String themeScript = getThemeScript(); | ||
String initScript = super.getInitScript(); | ||
return initScript + "\n" + """ | ||
function navigateToIssue(e, target) { | ||
e.preventDefault(); | ||
var filePath = target.getAttribute('file-path'); | ||
var startLine = target.getAttribute('start-line'); | ||
var endLine = target.getAttribute('end-line'); | ||
var startCharacter = target.getAttribute('start-character'); | ||
var endCharacter = target.getAttribute('end-character'); | ||
window.openInEditor(filePath, startLine, endLine, startCharacter, endCharacter); | ||
} | ||
var navigatableLines = document.getElementsByClassName('data-flow-clickable-row'); | ||
for(var i = 0; i < navigatableLines.length; i++) { | ||
navigatableLines[i].onclick = function(e) { | ||
navigateToIssue(e, this); | ||
return false; | ||
}; | ||
} | ||
if(document.getElementById('position-line')) { | ||
document.getElementById('position-line').onclick = function(e) { | ||
var target = navigatableLines[0]; | ||
if(target) { | ||
navigateToIssue(e, target); | ||
} | ||
} | ||
} | ||
// Disable Autofix and ignores | ||
if(document.getElementById('ai-fix-wrapper') && document.getElementById('no-ai-fix-wrapper')){ | ||
document.getElementById('ai-fix-wrapper').className = 'hidden'; | ||
document.getElementById('no-ai-fix-wrapper').className = ''; | ||
} | ||
if(document.getElementsByClassName('ignore-action-container') && document.getElementsByClassName('ignore-action-container')[0]){ | ||
ShawkyZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
document.getElementsByClassName('ignore-action-container')[0].className = 'hidden'; | ||
} | ||
""" + themeScript; | ||
} | ||
|
||
private String getThemeScript() { | ||
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); | ||
ITheme currentTheme = themeManager.getCurrentTheme(); | ||
String themeId = currentTheme.getId().toLowerCase(); | ||
|
||
boolean isDarkTheme = themeId.contains("dark"); | ||
boolean isHighContrast = themeId.contains("highcontrast") || themeId.contains("high-contrast"); | ||
|
||
String themeScript = "var isDarkTheme = " + isDarkTheme + ";\n" + | ||
"var isHighContrast = " + isHighContrast + ";\n" + | ||
"document.body.classList.add(isHighContrast ? 'high-contrast' : (isDarkTheme ? 'dark' : 'light'));"; | ||
return themeScript; | ||
} | ||
|
||
@Override | ||
public String replaceCssVariables(String html) { | ||
html = super.replaceCssVariables(html); | ||
|
||
// Replace CSS variables with actual color values | ||
html = html.replace("var(--example-line-removed-color)", super.getColorAsHex("org.eclipse.ui.workbench.lineRemovedColor", "#ff0000")); | ||
html = html.replace("var(--example-line-added-color)", super.getColorAsHex("org.eclipse.ui.workbench.lineAddedColor", "#00ff00")); | ||
|
||
return html; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
plugin/src/main/java/io/snyk/eclipse/plugin/html/HtmlProviderFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.snyk.eclipse.plugin.html; | ||
|
||
import io.snyk.eclipse.plugin.domain.ProductConstants; | ||
|
||
public class HtmlProviderFactory { | ||
|
||
public static BaseHtmlProvider GetHtmlProvider(String product) | ||
{ | ||
switch (product) { | ||
case ProductConstants.DISPLAYED_CODE_SECURITY: | ||
case ProductConstants.DISPLAYED_CODE_QUALITY: | ||
return CodeHtmlProvider.getInstance(); | ||
case ProductConstants.DISPLAYED_OSS: | ||
return OssHtmlProvider.getInstance(); | ||
case ProductConstants.DISPLAYED_IAC: | ||
return IacHtmlProvider.getInstance(); | ||
} | ||
return null; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
plugin/src/main/java/io/snyk/eclipse/plugin/html/IacHtmlProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.snyk.eclipse.plugin.html; | ||
|
||
public class IacHtmlProvider extends BaseHtmlProvider { | ||
private static IacHtmlProvider instance = new IacHtmlProvider(); | ||
public static IacHtmlProvider getInstance() { | ||
if (instance == null) { | ||
synchronized (IacHtmlProvider.class) { | ||
if (instance == null) { | ||
instance = new IacHtmlProvider(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
plugin/src/main/java/io/snyk/eclipse/plugin/html/OssHtmlProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.snyk.eclipse.plugin.html; | ||
|
||
public class OssHtmlProvider extends BaseHtmlProvider { | ||
private static OssHtmlProvider instance = new OssHtmlProvider(); | ||
public static OssHtmlProvider getInstance() { | ||
if (instance == null) { | ||
synchronized (OssHtmlProvider.class) { | ||
if (instance == null) { | ||
instance = new OssHtmlProvider(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
@Override | ||
public String replaceCssVariables(String html) { | ||
html = super.replaceCssVariables(html); | ||
html = html.replace("var(--container-background-color)", super.getColorAsHex("org.eclipse.ui.workbench.CODE_BACKGROUND_COLOR", "#F0F0F0")); | ||
|
||
return html; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.