All files / src/views CodeComparisonView.ts

97.51% Statements 196/201
100% Branches 2/2
100% Functions 2/2
97.51% Lines 196/201

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 2011x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x
import * as vscode from 'vscode';
import { globalContext } from '../extension';
import { escapeHtml } from '../utils';
 
/**
 * Creates and displays a Webview panel that shows a side-by-side comparison
 * of a correct and incorrect code snippet, along with an explanation.
 *
 * This is used to help users understand suggested improvements or corrections
 * by providing a visual, interactive comparison inside VS Code.
 *
 * @param rightCode - The correct or suggested version of the code.
 * @param wrongCode - The original, incorrect version of the code.
 * @param explanation - A textual explanation describing the correction.
 */
 
export const createCodeComparisonWebview = (rightCode: string, wrongCode: string, explanation: string) => {
    const panel = vscode.window.createWebviewPanel(
        'codeComparison',
        'Code Comparison',
        vscode.ViewColumn.Beside,
        {
            enableScripts: true,
            retainContextWhenHidden: true,
        }
    );
 
    panel.webview.html = getComparisonWebviewContent(rightCode, wrongCode, explanation);
 
    // Handle messages from the webview
    panel.webview.onDidReceiveMessage(
        message => {
            switch (message.command) {
                case 'closeWebview':
                    panel.dispose(); // Close the webview
                    break;
            }
        },
        undefined,
        globalContext.subscriptions
    );
};
 
/**
 * Generates the HTML content for the code comparison Webview.
 *
 * This function returns a fully-structured HTML page that displays:
 * - The correct code snippet
 * - The incorrect code snippet
 * - An explanation for the correction
 * - A footer button that closes the Webview
 *
 * @param rightCode - The correct or improved code snippet.
 * @param wrongCode - The original, incorrect code snippet.
 * @param explanation - An explanation of why the change is necessary.
 * @returns A string containing the HTML content for the Webview.
 */
const getComparisonWebviewContent = (rightCode: string, wrongCode: string, explanation: string): string => {
    return `
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Code Comparison</title>
            <style>
                :root {
                    --container-padding: 20px;
                    --section-padding: 16px;
                    --border-radius: 8px;
                    --border-color: #e1e4e8;
                    --section-margin: 16px;
                }
                
                body {
                    font-family: Arial, sans-serif;
                    margin: 0;
                    padding: var(--container-padding);
                    background-color: #0e0e0e;
                    color: #333;
                    display: flex;
                    flex-direction: column;
                    height: 100vh;
                    box-sizing: border-box;
                }
                
                .content {
                    flex: 1;
                    overflow: auto;
                    padding-bottom: 80px;
                }
                
                .code-block {
                    background: white;
                    padding: var(--section-padding);
                    margin-bottom: var(--section-margin);
                    border-radius: var(--border-radius);
                    border: 1px solid var(--border-color);
                    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
                }
                
                .code-content {
                    background-color: #f5f5f5;
                    font-family: 'Courier New', monospace;
                    white-space: pre-wrap;
                    padding: 12px;
                    border-radius: calc(var(--border-radius) - 2px);
                    overflow-x: auto;
                    margin-top: 8px;
                    border: 1px solid var(--border-color);
                }
                
                h3 {
                    margin: 0 0 8px 0;
                    color: #24292e;
                    font-size: 16px;
                }
 
                .explanation {
                    background: rgb(224, 236, 255);
                    padding: var(--section-padding);
                    margin-bottom: var(--section-margin);
                    border-radius: var(--border-radius);
                    border: 1px solid var(--border-color);
                    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
                    border-left: 4px solid rgb(14, 145, 2);
                }
                
                .correct-header {
                    color: #2e7d32;
                }
                
                .incorrect-header {
                    color: #c62828;
                }
                
                .footer {
                    position: fixed;
                    bottom: 0;
                    left: 0;
                    right: 0;
                    background-color: #0e0e0e;
                    padding: 16px var(--container-padding);
                    display: flex;
                    justify-content: flex-end;
                    box-shadow: 0 -2px 5px rgba(0,0,0,0.05);
                }
                
                .close-button {
                    padding: 8px 24px;
                    background-color: #4CAF50;
                    color: white;
                    border: none;
                    border-radius: var(--border-radius);
                    cursor: pointer;
                    font-size: 14px;
                    font-weight: 500;
                    transition: background-color 0.2s;
                }
                
                .close-button:hover {
                    background-color: #45a049;
                }
            </style>
        </head>
        <body>
            <div class="content">
                <div class="code-block">
                    <h3 class="correct-header">Correct Code</h3>
                    <div class="code-content">${escapeHtml(rightCode)}</div>
                </div>
                
                <div class="code-block">
                    <h3 class="incorrect-header">Incorrect Code</h3>
                    <div class="code-content">${escapeHtml(wrongCode)}</div>
                </div>
 
                <div class="explanation">
                    <h3>Explanation</h3>
                    <div>${escapeHtml(explanation)}</div>
                </div>
            </div>
            
            <div class="footer">
                <button class="close-button" id="closeButton">Got it</button>
            </div>
 
            <script>
                const vscode = acquireVsCodeApi();
                const closeButton = document.getElementById('closeButton');
                
                closeButton.addEventListener('click', () => {
                    vscode.postMessage({
                        command: 'closeWebview'
                    });
                });
            </script>
        </body>
        </html>
    `;
};