Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recursive Folding Block #555

Merged
merged 18 commits into from
Dec 5, 2023
Merged
70 changes: 10 additions & 60 deletions client/src/commands/run.ts
jingjiajie marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
import {
EventEmitter,
Position,
ProgressLocation,
Selection,
Uri,
Expand All @@ -25,15 +24,9 @@ import {
RunResult,
getSession,
} from "../connection";
import { getSelectedRegions } from "../utils/utils";
import { profileConfig, switchProfile } from "./profile";

interface FoldingBlock {
startLine: number;
startCol: number;
endLine: number;
endCol: number;
}

let running = false;

function getCode(selected = false, uri?: Uri): string {
Expand Down Expand Up @@ -76,57 +69,6 @@ function getCode(selected = false, uri?: Uri): string {
return wrapCodeWithOutputHtml(code);
}

async function getSelectedRegions(
client: BaseLanguageClient,
): Promise<Selection[]> {
const result: string[] = [];

async function pushBlock(line: number, col: number) {
const block = await client.sendRequest<FoldingBlock>(
"sas/getFoldingBlock",
{
textDocument: { uri: window.activeTextEditor.document.uri.toString() },
line,
col,
},
);
if (block) {
const start = doc.offsetAt(new Position(block.startLine, block.startCol));
const end = doc.offsetAt(new Position(block.endLine, block.endCol));
const key = `${start}-${end}`;
if (result.indexOf(key) === -1) {
result.push(key);
}
return end;
}
}

const editor = window.activeTextEditor;
const doc = editor.document;
for (const selection of editor.selections) {
const start = doc.offsetAt(selection.start);
let end = doc.offsetAt(selection.end);
const selectedText = doc.getText(selection);
if (selectedText.endsWith("\n")) {
--end;
}
for (let i = start; i <= end; i++) {
const pos = doc.positionAt(i);
const blockEnd = await pushBlock(pos.line, pos.character);
if (blockEnd && blockEnd > i) {
i = blockEnd;
}
}
}
return result.map((key) => {
const [start, end] = key.split("-");
return new Selection(
doc.positionAt(parseInt(start)),
doc.positionAt(parseInt(end)),
);
});
}

async function runCode(selected?: boolean, uri?: Uri) {
if (profileConfig.getActiveProfile() === "") {
switchProfile();
Expand Down Expand Up @@ -193,7 +135,15 @@ export async function runSelected(uri: Uri): Promise<void> {

export async function runRegion(client: BaseLanguageClient): Promise<void> {
const selections = await getSelectedRegions(client);
window.activeTextEditor.selections = selections;
const editor = window.activeTextEditor;
const doc = editor.document;
if (selections.length === 0) {
editor.selections = [
jingjiajie marked this conversation as resolved.
Show resolved Hide resolved
new Selection(doc.positionAt(0), doc.positionAt(doc.getText().length)),
];
} else {
editor.selections = selections;
}
await _run(true);
}

Expand Down
2 changes: 2 additions & 0 deletions client/src/node/extension.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks unnecessary to change this file now

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { NotebookSerializer } from "../components/notebook/Serializer";
import { ConnectionType } from "../components/profile";
import { SasTaskProvider } from "../components/tasks/SasTaskProvider";
import { SAS_TASK_TYPE } from "../components/tasks/SasTasks";
import { getSelectedRegions } from "../utils/utils";
jingjiajie marked this conversation as resolved.
Show resolved Hide resolved

let client: LanguageClient;
// Create Profile status bar item
Expand Down Expand Up @@ -131,6 +132,7 @@ export function activate(context: ExtensionContext): void {
"SAS",
new SASAuthProvider(context.secrets),
),

languages.registerDocumentSemanticTokensProvider(
{ language: "sas-log" },
LogTokensProvider,
Expand Down
60 changes: 60 additions & 0 deletions client/src/utils/utils.ts
jingjiajie marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Position, Selection, window } from "vscode";
import { BaseLanguageClient } from "vscode-languageclient";

interface FoldingBlock {
startLine: number;
startCol: number;
endLine: number;
endCol: number;
}

export async function getSelectedRegions(
client: BaseLanguageClient,
): Promise<Selection[]> {
const result: string[] = [];

async function pushBlock(line: number, col: number) {
const block = await client.sendRequest<FoldingBlock>(
"sas/getFoldingBlock",
{
textDocument: { uri: window.activeTextEditor.document.uri.toString() },
line,
col,
},
);
if (block) {
const start = doc.offsetAt(new Position(block.startLine, block.startCol));
const end = doc.offsetAt(new Position(block.endLine, block.endCol));
const key = `${start}-${end}`;
if (result.indexOf(key) === -1) {
result.push(key);
}
return end;
}
}

const editor = window.activeTextEditor;
const doc = editor.document;
for (const selection of editor.selections) {
const start = doc.offsetAt(selection.start);
let end = doc.offsetAt(selection.end);
const selectedText = doc.getText(selection);
if (selectedText.endsWith("\n")) {
--end;
}
for (let i = start; i <= end; i++) {
const pos = doc.positionAt(i);
const blockEnd = await pushBlock(pos.line, pos.character);
if (blockEnd && blockEnd > i) {
i = blockEnd;
}
}
}
return result.map((key) => {
const [start, end] = key.split("-");
return new Selection(
doc.positionAt(parseInt(start)),
doc.positionAt(parseInt(end)),
);
});
}
11 changes: 10 additions & 1 deletion server/src/sas/CodeZoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ export class CodeZoneManager {
block = this._syntaxProvider.getFoldingBlock(
tmpContext.line,
tmpContext.col,
false,
true,
true,
);
if (block) {
if (this._inBlock(block, token)! < 0 && !this._endedReally(block)) {
Expand Down Expand Up @@ -2428,7 +2431,13 @@ export class CodeZoneManager {
const tmpLine = pos.line,
tmpCol = pos.col;
let token = this._token(tmpLine, tmpCol)!;
const block = this._syntaxProvider.getFoldingBlock(tmpLine, tmpCol);
const block = this._syntaxProvider.getFoldingBlock(
tmpLine,
tmpCol,
false,
true,
true,
);
/* first check type to determine zone, some special conditions
* 1) for bringing up auto completion popup by shortcut,
* 2) input at the end of a line in comment or literal
Expand Down
8 changes: 7 additions & 1 deletion server/src/sas/FormatOnTypeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ export class FormatOnTypeProvider {
}
// Otherwise, need to decrease indent of current line
const foldingBlock: FoldingBlock | null =
this.syntaxProvider.getFoldingBlock(line, semicolonCol);
this.syntaxProvider.getFoldingBlock(
line,
semicolonCol,
false,
true,
true,
);
let blockStartLine;
if (!foldingBlock) {
const lastNotEmptyLine = this._getLastNotEmptyLine(line - 1);
Expand Down
Loading