Skip to content

Commit

Permalink
feat: Outdent code when running selection from a notebook (#1391)
Browse files Browse the repository at this point in the history
Fixes #1326
  • Loading branch information
mattrunyon authored Jul 6, 2023
1 parent 3a89bbf commit 154ccfc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/console/src/notebook/ScriptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Editor from './Editor';
import { MonacoProviders, MonacoUtils } from '../monaco';
import './ScriptEditor.scss';
import SHORTCUTS from '../ConsoleShortcuts';
import ScriptEditorUtils from './ScriptEditorUtils';

const log = Log.module('ScriptEditor');

Expand Down Expand Up @@ -133,12 +134,13 @@ class ScriptEditor extends Component<ScriptEditorProps, ScriptEditorState> {
if (endColumn === 1 && endLineNumber > startLineNumber) {
endLineNumber -= 1;
}
const startLineMinColumn = model?.getLineMinColumn(startLineNumber);
const endLineMaxColumn = model?.getLineMaxColumn(endLineNumber);
const startLineMinColumn = model.getLineMinColumn(startLineNumber);
const endLineMaxColumn = model.getLineMaxColumn(endLineNumber);
const wholeLineRange = range
.setStartPosition(startLineNumber, startLineMinColumn)
.setEndPosition(endLineNumber, endLineMaxColumn);
return model?.getValueInRange(wholeLineRange);

return ScriptEditorUtils.outdentCode(model.getValueInRange(wholeLineRange));
}

handleEditorInitialized(innerEditor: editor.IStandaloneCodeEditor): void {
Expand Down
69 changes: 69 additions & 0 deletions packages/console/src/notebook/ScriptEditorUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import ScriptEditorUtils from './ScriptEditorUtils';

describe('normalize script language', () => {
test.each([
['groovy', 'Groovy'],
['python', 'Python'],
['scala', 'Scala'],
['other', null],
])('%s', (param, expected) =>
expect(ScriptEditorUtils.normalizeScriptLanguage(param as 'python')).toBe(
expected
)
);
});

describe('get disabled run tooltip', () => {
test('not connected', () =>
expect(
ScriptEditorUtils.getDisabledRunTooltip(false, false, 'lang', 'test')
).toMatch('not connected'));

test('session language does not match', () =>
expect(
ScriptEditorUtils.getDisabledRunTooltip(true, false, 'lang', 'test')
).toMatch("lang doesn't match"));

test('not disabled', () =>
expect(
ScriptEditorUtils.getDisabledRunTooltip(true, true, 'lang', 'test')
).toBeNull());
});

describe('outdent code', () => {
test.each([
{ input: '\n\n\n', output: '\n\n\n', case: 'empty lines' },
{
input: 'a\n b\n c\nd',
output: 'a\n b\n c\nd',
case: 'some lines not indented',
},
{
input: ' a\n b\n c',
output: 'a\nb\nc',
case: 'all lines same indent',
},
{
input: '\n\n a\n\n b\n c',
output: '\n\na\n\n b\nc',
case: 'blank lines in the code',
},
{
input: '',
output: '',
case: 'empty string',
},
{
input: '\ta\n\t\tb\n\tc',
output: 'a\n\tb\nc',
case: 'tab indents',
},
{
input: 'a\nb\nc\n',
output: 'a\nb\nc\n',
case: 'no indents',
},
])('outdentCode with $case', ({ input, output }) => {
expect(ScriptEditorUtils.outdentCode(input)).toBe(output);
});
});
18 changes: 18 additions & 0 deletions packages/console/src/notebook/ScriptEditorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ class ScriptEditorUtils {
}
return null;
}

/**
* Outdent (opposite of indent) a string so the lowest indent level is 0
* Assumes each line is indented with the same characters (i.e. no mixed tabs/spaces)
* @param code The code to outdent
* @returns A code block where the minimum indent level of a line is 0
*/
static outdentCode(code: string) {
const lines = code.split('\n');
const minIndent = lines.reduce((min, line) => {
const indentLength = line.length - line.trimStart().length;
if (indentLength === line.length) {
return min;
}
return indentLength < min ? indentLength : min;
}, Number.MAX_SAFE_INTEGER);
return lines.map(line => line.slice(minIndent)).join('\n');
}
}

export default ScriptEditorUtils;

0 comments on commit 154ccfc

Please sign in to comment.