Skip to content

Commit b4e5512

Browse files
author
Ben Lerner
committed
Subsumes #516. Fixes #399.
Is more restrictive about guessing when the first line is a context line, and trims the context name of leading/trailing spaces as well.
1 parent 2be08b7 commit b4e5512

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/web/js/beforePyret.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ window.CPO = {
125125
};
126126
$(function() {
127127
const CONTEXT_FOR_NEW_FILES = "use context starter2024\n";
128+
const CONTEXT_PREFIX = /^use context\s+/;
129+
128130
function merge(obj, extension) {
129131
var newobj = {};
130132
Object.keys(obj).forEach(function(k) {
@@ -222,7 +224,7 @@ $(function() {
222224

223225
function firstLineIsNamespace() {
224226
const firstline = CM.getLine(0);
225-
const match = firstline.match(/^use context.*/);
227+
const match = firstline.match(CONTEXT_PREFIX);
226228
return match !== null;
227229
}
228230

@@ -485,11 +487,14 @@ $(function() {
485487
});
486488
namespaceResult.show((result) => {
487489
if(!result) { return; }
488-
if(result.match(/^use context*/)) { result = result.slice("use context ".length); }
489-
CPO.editor.setContextLine("use context " + result + "\n");
490+
CPO.editor.setContextLine("use context " + result.trim() + "\n");
490491
});
491492
}
492-
$("#choose-context").on("click", function() { showModal(CPO.editor.cm.getLine(0).slice("use context ".length)); });
493+
$("#choose-context").on("click", function() {
494+
const firstLine = CPO.editor.cm.getLine(0);
495+
const contextLen = firstLine.match(CONTEXT_PREFIX);
496+
showModal(contextLen === null ? "" : firstLine.slice(contextLen[0].length));
497+
});
493498

494499
var TRUNCATE_LENGTH = 20;
495500

0 commit comments

Comments
 (0)