Skip to content
Open
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
16 changes: 12 additions & 4 deletions client/modules/IDE/components/Editor/stateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,22 @@ export function createNewFileState(filename, document, settings) {
const autocompleteCpt = new Compartment();

// Depending on the file mode, we have a different tidier function.
// Keep this binding local to each file state so modes don't accumulate
// across files via a shared module-level array.
const mode = getFileMode(filename);
extraKeymaps.push({
key: `Shift-Mod-F`,
run: (cmView) => tidyCodeWithPrettier(cmView, mode)
});
const fileTidyKeymap = [
{
key: 'Shift-Mod-F',
run: (cmView) => {
tidyCodeWithPrettier(cmView, mode);
return true;
}
}
];

const keymaps = [
extraKeymaps,
fileTidyKeymap,
closeBracketsKeymap,
defaultKeymap,
historyKeymap,
Expand Down
62 changes: 62 additions & 0 deletions client/modules/IDE/components/Editor/stateUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @jest-environment jsdom
*/
import { EditorView, runScopeHandlers } from '@codemirror/view';
import { createNewFileState } from './stateUtils';

const defaultSettings = {
linewrap: false,
lineNumbers: false,
autocomplete: false,
autocloseBracketsQuotes: false,
onUpdateLinting: () => {},
onViewUpdate: () => {},
referenceBaseUrl: 'https://p5js.org'
};

function mountFile(filename, doc) {
const { cmState } = createNewFileState(filename, doc, defaultSettings);
const parent = document.createElement('div');
document.body.appendChild(parent);
const view = new EditorView({ state: cmState, parent });
return view;
}

function pressTidyShortcut(view) {
const event = new KeyboardEvent('keydown', {
key: 'F',
code: 'KeyF',
shiftKey: true,
ctrlKey: true
});
runScopeHandlers(view, event, 'editor');
}

describe('createNewFileState — Tidy keyboard shortcut', () => {
afterEach(() => {
document.body.innerHTML = '';
});

// Regression test for https://github.com/processing/p5.js-web-editor/issues/4093
// Each file's Shift-Mod-F binding must only tidy with its own mode, regardless
// of the order in which the file states were created.
it('tidies each file with its own mode regardless of creation order', () => {
const jsView = mountFile('sketch.js', `function foo(){console.log("hi")}`);
const cssView = mountFile('style.css', `body{margin:0;padding:0;}`);
const htmlView = mountFile('index.html', `<html><body>hello</body></html>`);

pressTidyShortcut(jsView);
pressTidyShortcut(cssView);
pressTidyShortcut(htmlView);

expect(jsView.state.doc.toString()).toBe(
'function foo() {\n console.log("hi");\n}\n'
);
expect(cssView.state.doc.toString()).toBe(
'body {\n margin: 0;\n padding: 0;\n}\n'
);
expect(htmlView.state.doc.toString()).toBe(
'<html>\n <body>\n hello\n </body>\n</html>\n'
);
});
});