better current query UX

This commit is contained in:
Jan Prochazka 2021-12-12 11:56:48 +01:00
parent 6930a2543c
commit 42d7166d2b
3 changed files with 34 additions and 30 deletions

View File

@ -40,6 +40,8 @@ export interface SplitResultItemRich {
text: string;
start: SplitPositionDefinition;
end: SplitPositionDefinition;
trimStart?: SplitPositionDefinition;
trimEnd?: SplitPositionDefinition;
}
export type SplitResultItem = string | SplitResultItemRich;
@ -211,7 +213,7 @@ function pushQuery(context: SplitLineContext) {
if (trimmed) {
if (context.options.returnRichInfo) {
context.pushOutput(
trimPositions(sql, {
countTrimmedPositions(sql, {
text: trimmed,
start: {
@ -233,26 +235,26 @@ function pushQuery(context: SplitLineContext) {
}
}
function trimPositions(full: string, positions: SplitResultItemRich): SplitResultItemRich {
function countTrimmedPositions(full: string, positions: SplitResultItemRich): SplitResultItemRich {
const startIndex = full.indexOf(positions.text);
const res = {
...positions,
start: {
...positions.start,
},
};
const trimStart = { ...positions.start };
for (let i = 0; i < startIndex; i += 1) {
if (full[i] == '\n') {
res.start.position += 1;
res.start.line += 1;
res.start.column = 0;
trimStart.position += 1;
trimStart.line += 1;
trimStart.column = 0;
} else {
res.start.position += 1;
res.start.column += 1;
trimStart.position += 1;
trimStart.column += 1;
}
}
return res;
return {
...positions,
trimStart,
trimEnd: positions.end,
};
}
function markStartCommand(context: SplitLineContext) {
@ -330,7 +332,7 @@ export function finishSplitStream(context: SplitStreamContext) {
if (trimmed) {
if (context.options.returnRichInfo) {
context.pushOutput(
trimPositions(context.commandPart, {
countTrimmedPositions(context.commandPart, {
text: trimmed,
start: {
@ -364,12 +366,16 @@ export function splitQuery(sql: string, options: SplitterOptions = null): SplitR
return [
{
text: sql,
startLine: 0,
startPosition: 0,
startColumn: 0,
endLine: lines.length,
endColumn: lines[lines.length - 1]?.length || 0,
endPosition: sql.length,
start: {
position: 0,
line: 0,
column: 0,
},
end: {
position: sql.length,
line: lines.length,
column: lines[lines.length - 1]?.length || 0,
},
},
];
}

View File

@ -100,7 +100,7 @@ test('count lines', () => {
expect.objectContaining({
text: 'SELECT * FROM `table1`',
start: expect.objectContaining({
trimStart: expect.objectContaining({
position: 0,
line: 0,
column: 0,
@ -115,7 +115,7 @@ test('count lines', () => {
expect.objectContaining({
text: 'SELECT * FROM `table2`',
start: expect.objectContaining({
trimStart: expect.objectContaining({
position: 24,
line: 1,
column: 0,
@ -141,7 +141,7 @@ test('count lines with flush', () => {
expect.objectContaining({
text: 'SELECT * FROM `table1`',
start: expect.objectContaining({
trimStart: expect.objectContaining({
position: 0,
line: 0,
column: 0,
@ -156,7 +156,7 @@ test('count lines with flush', () => {
expect.objectContaining({
text: 'SELECT * FROM `table2`',
start: expect.objectContaining({
trimStart: expect.objectContaining({
position: 24,
line: 1,
column: 0,

View File

@ -189,13 +189,11 @@
currentPart = part;
if (currentPart) {
const start = currentPart.trimStart || currentPart.start;
const end = currentPart.trimEnd || currentPart.end;
currentPartMarker = editor
.getSession()
.addMarker(
new ace.Range(currentPart.start.line, currentPart.start.column, currentPart.end.line, currentPart.end.column),
'ace_active-line',
'text'
);
.addMarker(new ace.Range(start.line, start.column, end.line, end.column), 'ace_active-line', 'text');
}
}
}