Skip to content
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

Proper fix for changeRange() shrinking lines / Ctrl-L to clear REPL Window / Fix issue 303 #435

Merged
merged 8 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Change Log
Changes to Calva.

## [Unreleased]
## [Unreleased]
- [REPL colours and logo get too much attention](https://github.com/BetterThanTomorrow/calva/issues/303)

## [2.0.55] - 2019-10-27
- [Add commands for interrupting the current evaluation as well as all running evaluations](https://github.com/BetterThanTomorrow/calva/issues/237)
Expand Down
32 changes: 29 additions & 3 deletions assets/styles/webview.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ img {
.logo {
img.logo {
margin-right: 10px;
height: 64px;
height: 40px;
}
}
.powered {
Expand All @@ -26,8 +26,8 @@ img {
align-items: center;
img {
flex-shrink: 2;
max-height: 64px;
min-height: 40px;
max-height: 40px;
min-height: 20px;
margin-right: 8px;
margin-left: 8px;
}
Expand Down Expand Up @@ -61,6 +61,32 @@ img {
}
}

p.hint {
width: 550px;
}

ul.commands {
display: inline-block;
width: 400px;
margin: 0 0;
}

ul.commands li.left {
float: left;
list-style: circle none none;
width: 25%;
}

ul.commands li.right {
float: left;
list-style: outside none none;
width: 75%;
}

span.code {
color: var(--vscode-textPreformat-foreground);
}

.userinput {
color: var(--vscode-editor-foreground);
background-color: var(--vscode-editor-background);
Expand Down
42 changes: 20 additions & 22 deletions assets/webview.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,46 @@
<html>

<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; connect-src vscode-resource:; script-src 'unsafe-inline' 'unsafe-eval' data: vscode-resource:; style-src 'unsafe-inline' data: vscode-resource:; img-src data: vscode-resource:; font-src 'unsafe-inline' data: vscode-resource:">
<meta http-equiv="Content-Security-Policy"
content="default-src 'none'; connect-src {{csp-source}}; script-src 'unsafe-inline' 'unsafe-eval' data: {{csp-source}}; style-src 'unsafe-inline' data: {{csp-source}}; img-src data: {{csp-source}}; font-src 'unsafe-inline' data: {{csp-source}}">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
</head>

<body>
<div>
<div id="hero" class="{{hero-classes}}">
<div class="logo">
<img class="logo" src="{{logo-symbol}}">
<a href="https://calva.readthedocs.io/">
<img class="logo" src="{{logo-symbol}}" alt="Calva" title="Calva User Documentation">
</a>
</div>
<div class="powered">
<div class="type clj">
<img class="clj-logo" src="{{clj-logo}}">
<img class="clj-logo" src="{{clj-logo}}" alt="Clojure" title="Clojure">
<div class="type-card">
<img class="clj-type-logo" src="{{clj-type-logo}}">
<p>{{clj-type}}</p>
<img class="clj-type-logo" src="{{clj-type-logo}}" alt="{{clj-type}}" title="{{clj-type}}">
</div>
</div>
<div class="type cljs">
<img class="cljs-logo" src="{{cljs-logo}}">
<img class="cljs-logo" src="{{cljs-logo}}" alt="ClojureScript" title="ClojureScript">
<div class="type-card {{hero-classes}}">
<img class="cljs-type-logo" src="{{cljs-type-logo}}">
<p>{{cljs-type}}</p>
<img class="cljs-type-logo" src="{{cljs-type-logo}}" alt="{{cljs-type}}" title="{{cljs-type}}">
</div>
</div>
</div>
</div>
<h3>Calva REPL – Powered by <a href="https://github.com/clojure-emacs/orchard">The Orchard</a>.</h3>
<p class="warn">
<p>Please report any issues you find on
<a href="https://github.com/BetterThanTomorrow/calva/issues">GitHub</a>. Also feel welcome to chat us up on the Clojurians Slack, we're in <a href="https://clojurians.slack.com/messages/calva-dev/">#calva-dev</a>.</p>
</p>
<!-- <p id="motd"></p> -->
<p>The REPL prompt is a multiline, Paredit powered, Clojure editor. Some useful shortcuts are:
<ul>
<li><code>alt+up/down</code>&nbsp; for history navigation</li>
<li><code>alt+enter</code>&nbsp; to submit the current line</li>
<li><code>ctrl+d</code>&nbsp; to interrupt a running evaluation</li>
<p class="hint">The REPL prompt is a multiline, Paredit powered, Clojure editor. Some useful shortcuts are:
<ul class="commands">
<li class="left"><span class="code">Alt+Up/Down</span></li><li class="right">Navigate the history</li>
<li class="left"><span class="code">Alt+Enter</span></li><li class="right">Submit the current line</li>
<li class="left"><span class="code">Ctrl+D</span></li><li class="right">Interrupt a running evaluation</li>
<li class="left"><span class="code">Ctrl+L</span></li><li class="right">Clear the REPL window</li>
</ul>
</p>
<p><strong>NB:</strong> Make it a habit to start any Calva session by loading a file. Many things do not work before that is done. Use <code>ctrl+alt+c enter</code> in the code editor to load (evaluate) the active file.</p>
<p>See also the <a href="https://calva.readthedocs.io/">Calva User Documentation</a>.</p>
<p class="hint">Make it a habit to start any Calva session by loading a file.
Use <span class="code">Ctrl+Alt+C Enter</span> in the code editor to load (evaluate) the active file.
Many things do not work before that is done.
</p>
<p class="hint">See also the <a href="https://calva.readthedocs.io/">Calva User Documentation</a>.</p>
</div>
<div id="repl" class="repl">
</div>
Expand Down
7 changes: 4 additions & 3 deletions src/repl-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ class REPLWindow {
const cljTypeSlug = `clj-type-${cljType.replace(/ /, "-").toLowerCase()}`;
const cljsTypeSlug = `cljs-type-${cljsType.replace(/ /, "-").toLowerCase()}`;
let html = fs.readFileSync(path.join(ctx.extensionPath, "assets/webview.html")).toString();
let script = panel.webview.asWebviewUri(vscode.Uri.file(path.join(ctx.extensionPath, "out/webview.js"))).toString()
let script = panel.webview.asWebviewUri(vscode.Uri.file(path.join(ctx.extensionPath, "out/webview.js"))).toString();
html = html.replace("{{script}}", script);
html = html.replace(/{{csp-source}}/g, panel.webview.cspSource);
html = html.replace("{{logo-symbol}}", panel.webview.asWebviewUri(getImageUrl(`calva-symbol-logo.svg`)).toString());
html = html.replace(/{{hero-classes}}/g, `${type} ${cljTypeSlug} ${cljsTypeSlug}`);
html = html.replace("{{clj-type}}", `${cljType.replace(/ /g, "&nbsp;")}`);
html = html.replace(/{{clj-type}}/g, `${cljType.replace(/ /g, "&nbsp;")}`);
html = html.replace("{{clj-type-logo}}", panel.webview.asWebviewUri(getImageUrl(`${cljTypeSlug}.svg`)).toString());
html = html.replace("{{clj-logo}}", panel.webview.asWebviewUri(getImageUrl(`clj.svg`)).toString());
html = html.replace("{{cljs-type}}", `${cljsType.replace(/ /g, "&nbsp;")}`);
html = html.replace(/{{cljs-type}}/g, `${cljsType.replace(/ /g, "&nbsp;")}`);
html = html.replace("{{cljs-type-logo}}", panel.webview.asWebviewUri(getImageUrl((`${cljsTypeSlug}.svg`))).toString());
html = html.replace("{{cljs-logo}}", panel.webview.asWebviewUri(getImageUrl(`cljs.svg`)).toString());
panel.webview.html = html;
Expand Down
2 changes: 1 addition & 1 deletion src/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ window.onmessage = (msg) => {
removeUserInput();
ns = msg.data.ns;
con.setHistory(msg.data.history);
con.clear();
con.commands["clear-window"]
con.requestPrompt(ns + "=> ")
}

Expand Down
24 changes: 15 additions & 9 deletions src/webview/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,30 +239,36 @@ export class LineInputModel {
// initialize the lexer state - the first line is definitely not in a string, otherwise copy the
// end state of the previous line before the edit
let state = this.getStateForLine(startLine)

if(startLine != endLine)
this.deleteLines(startLine+1, endLine-startLine - (replaceLines.length-1));
let currentLength = (endLine - startLine) + 1;

if(replaceLines.length == 1) {
// trivial single line edit
items.push(new TextLine(left + replaceLines[0] + right, state));
this.changedLines.add(startLine);
} else {
// multi line edit.
items.push(new TextLine(left + replaceLines[0], state));
for(let i=1; i<replaceLines.length-1; i++)
items.push(new TextLine(replaceLines[i], scanner.state));
items.push(new TextLine(replaceLines[replaceLines.length-1] + right, scanner.state))
this.insertLines(startLine+1, replaceLines.length-1 - (endLine-startLine))
for(let i=1; i<items.length; i++)
this.changedLines.add(startLine+i);
this.markDirty(startLine+1);
}

if(currentLength > replaceLines.length) {
// shrink the lines
this.deleteLines(startLine + replaceLines.length, currentLength - replaceLines.length);
} else if(currentLength < replaceLines.length) {
// extend the lines
this.insertLines(endLine, replaceLines.length - currentLength);
}

// now splice in our edited lines
this.lines.splice(startLine, endLine-startLine+1, ...items);
this.markDirty(startLine);

// set the changed and dirty marker
for(let i=0; i<items.length; i++) {
this.changedLines.add(startLine+i);
this.markDirty(startLine+1);
}

if(this.recordingUndo) {
this.undoManager.addUndoStep(new EditorUndoStep("Edit", start, text, deletedText, oldSelection, newSelection))
}
Expand Down
34 changes: 9 additions & 25 deletions src/webview/repl-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,6 @@ export class ReplConsole {
this.readline.mainElem.scrollIntoView({ block: "end" })
}

clear() {
this.readline.clearCompletion();
while(this.elem.firstChild) {
this.elem.removeChild(this.elem.firstChild);
}
this.setText("");
this.readline.freeze();
this.readline.repaint();
}

onRepaint = () => { };

commands = {
Expand Down Expand Up @@ -638,28 +628,22 @@ export class ReplConsole {
this.historyIndex = this.history.length;
this.historyIndex--;
let line = this.history[this.historyIndex] || "";
// this is a hack because there is a bug in the repaint method.
// 1. Clear the model and repaint.
this.readline.model.changeRange(0, this.readline.model.maxOffset, "");
this.readline.repaint();
// 2. Set the new line and set the selection at the end of the line.
this.readline.model.changeRange(0, this.readline.model.maxOffset, line);
this.readline.selectionStart = this.readline.selectionEnd = line.length;
this.readline.repaint();
this.readline.withUndo(() => {
this.readline.model.changeRange(0, this.readline.model.maxOffset, line);
this.readline.selectionStart = this.readline.selectionEnd = line.length;
})
this.readline.repaint();
},
"history-down": () => {
if (this.historyIndex == this.history.length || this.historyIndex == -1)
return;
this.historyIndex++;
let line = this.history[this.historyIndex] || "";
// this is a hack because there is a bug in the repaint method.
// 1. Clear the model and repaint.
this.readline.model.changeRange(0, this.readline.model.maxOffset, "");
this.readline.withUndo(() => {
this.readline.model.changeRange(0, this.readline.model.maxOffset, line);
this.readline.selectionStart = this.readline.selectionEnd = line.length;
})
this.readline.repaint();
// 2. Set the new line and set the selection at the end of the line.
this.readline.model.changeRange(0, this.readline.model.maxOffset, line);
this.readline.selectionStart = this.readline.selectionEnd = line.length;
this.readline.repaint();
},
"submit": () => {
this.submitLine(true, false)
Expand Down