Skip to content

Commit

Permalink
- fix use of token iterator
Browse files Browse the repository at this point in the history
- remove ace ranges from sense editor spec and fix spec 🤦
  • Loading branch information
jloleysens committed Dec 5, 2019
1 parent 3d0259f commit dd2f3e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
Expand Up @@ -20,15 +20,11 @@ import '../sense_editor.test.mocks';

import $ from 'jquery';
import _ from 'lodash';
import ace from 'brace';
import 'brace/mode/json';

import { create } from '../create';
const editorInput1 = require('./editor_input1.txt');
const utils = require('../../../../lib/utils/utils');

const aceRange = ace.acequire('ace/range');

describe('Editor', () => {
let input;

Expand All @@ -55,12 +51,8 @@ describe('Editor', () => {
let testCount = 0;

const callWithEditorMethod = (editorMethod, fn) => async (done) => {
try {
const results = await input[editorMethod]();
fn(results, done);
} catch {
done();
}
const results = await input[editorMethod]();
fn(results, done);
};

function utilsTest(name, prefix, data, testToRun) {
Expand Down Expand Up @@ -126,8 +118,7 @@ describe('Editor', () => {
simpleRequest.prefix,
simpleRequest.data,
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 3, 1);
compareRequest(range, expected);
compareRequest(range, { start: { lineNumber: 1, column: 1 }, end: { lineNumber: 4, column: 2 } });
done();
})
);
Expand All @@ -152,8 +143,10 @@ describe('Editor', () => {
' ' + simpleRequest.prefix,
simpleRequest.data,
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 3, 1);
expect(range).toEqual(expected);
expect(range).toEqual({
start: { lineNumber: 1, column: 1 },
end: { lineNumber: 4, column: 2 }
});
done();
})
);
Expand All @@ -180,8 +173,10 @@ describe('Editor', () => {
simpleRequest.prefix + ' ',
simpleRequest.data + ' ',
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 3, 1);
compareRequest(range, expected);
compareRequest(range, {
start: { lineNumber: 1, column: 1 },
end: { lineNumber: 4, column: 2 }
});
done();
})
);
Expand All @@ -208,8 +203,10 @@ describe('Editor', () => {
singleLineRequest.prefix,
singleLineRequest.data,
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 1, 32);
compareRequest(range, expected);
compareRequest(range, {
start: { lineNumber: 1, column: 1 },
end: { lineNumber: 2, column: 33 }
});
done();
})
);
Expand All @@ -234,8 +231,10 @@ describe('Editor', () => {
getRequestNoData.prefix,
'\n',
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 0, 10);
compareRequest(range, expected);
compareRequest(range, {
start: { lineNumber: 1, column: 1 },
end: { lineNumber: 1, column: 11 },
});
done();
})
);
Expand All @@ -260,8 +259,10 @@ describe('Editor', () => {
getRequestNoData.prefix,
getRequestNoData.data,
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 0, 10);
expect(range).toEqual(expected);
expect(range).toEqual({
start: { lineNumber: 1, column: 1 },
end: { lineNumber: 1, column: 11 },
});
done();
})
);
Expand All @@ -286,8 +287,10 @@ describe('Editor', () => {
multiDocRequest.prefix,
multiDocRequest.data,
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 2, 14);
expect(range).toEqual(expected);
expect(range).toEqual({
start: { lineNumber: 1, column: 1 },
end: { lineNumber: 3, column: 15 },
});
done();
})
);
Expand Down Expand Up @@ -323,8 +326,10 @@ describe('Editor', () => {
scriptRequest.prefix,
scriptRequest.data,
callWithEditorMethod('getRequestRange', (range, done) => {
const expected = new aceRange.Range(0, 0, 5, 1);
compareRequest(range, expected);
compareRequest(range, {
start: { lineNumber: 1, column: 1 },
end: { lineNumber: 6, column: 2 },
});
done();
})
);
Expand Down
Expand Up @@ -130,14 +130,14 @@ export class SenseEditor {
}
};

getRequestRange = async (row?: number): Promise<Range | null> => {
getRequestRange = async (lineNumber?: number): Promise<Range | null> => {
await this.coreEditor.waitForLatestTokens();

if (this.parser.isInBetweenRequestsRow(row)) {
if (this.parser.isInBetweenRequestsRow(lineNumber)) {
return null;
}

const reqStart = this.prevRequestStart(row);
const reqStart = this.prevRequestStart(lineNumber);
const reqEnd = this.nextRequestEnd(reqStart);

return {
Expand Down Expand Up @@ -260,8 +260,7 @@ export class SenseEditor {
// if the url row ends with some spaces, skip them.
t = this.parser.nextNonEmptyToken(tokenIter);
}

let bodyStartLineNumber = (t ? 0 : 1) + tokenIter.getCurrentTokenLineNumber()!; // artificially increase end of docs.
let bodyStartLineNumber = (t ? 0 : 1) + tokenIter.getCurrentPosition().lineNumber; // artificially increase end of docs.
let dataEndPos: Position;
while (
bodyStartLineNumber < range.end.lineNumber ||
Expand Down Expand Up @@ -391,7 +390,7 @@ export class SenseEditor {
pos = pos || this.coreEditor.getCurrentPosition();
const maxLines = this.coreEditor.getLineCount();
let curLineNumber = pos.lineNumber;
for (; curLineNumber < maxLines - 1; curLineNumber++) {
for (; curLineNumber <= maxLines; ++curLineNumber) {
const curRowMode = this.parser.getRowParseMode(curLineNumber);
// eslint-disable-next-line no-bitwise
if ((curRowMode & this.parser.MODE.REQUEST_END) > 0) {
Expand Down
Expand Up @@ -55,7 +55,7 @@ export default class RowParser {
if (line.indexOf('}', line.length - 1) >= 0) {
// check for a multi doc request (must start a new json doc immediately after this one end.
lineNumber++;
if (lineNumber < linesCount) {
if (lineNumber < linesCount + 1) {
line = (this.editor.getLineValue(lineNumber) || '').trim();
if (line.indexOf('{') === 0) {
// next line is another doc in a multi doc
Expand All @@ -69,7 +69,7 @@ export default class RowParser {

// check for single line requests
lineNumber++;
if (lineNumber >= linesCount) {
if (lineNumber >= linesCount + 1) {
// eslint-disable-next-line no-bitwise
return MODE.REQUEST_START | MODE.REQUEST_END;
}
Expand Down Expand Up @@ -135,7 +135,9 @@ export default class RowParser {

nextNonEmptyToken(tokenIter: TokenIterator) {
let t = tokenIter.stepForward();
while (t && this.isEmptyToken(t)) t = tokenIter.stepForward();
while (t && this.isEmptyToken(t)) {
t = tokenIter.stepForward();
}
return t;
}

Expand Down

0 comments on commit dd2f3e1

Please sign in to comment.