-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add library support for SASPy profile
Signed-off-by: Shuguang Sun <[email protected]>
- Loading branch information
1 parent
08db8b2
commit d8d4012
Showing
9 changed files
with
471 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright © 2024, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { commands } from "vscode"; | ||
|
||
import { SASPYSession } from "."; | ||
import { LogLine, getSession } from ".."; | ||
import { useRunStore } from "../../store"; | ||
|
||
class CodeRunner { | ||
public async runCode( | ||
code: string, | ||
startTag: string = "", | ||
endTag: string = "", | ||
): Promise<string> { | ||
// If we're already executing code, lets wait for it | ||
// to finish up. | ||
let unsubscribe; | ||
if (useRunStore.getState().isExecutingCode) { | ||
await new Promise((resolve) => { | ||
unsubscribe = useRunStore.subscribe( | ||
(state) => state.isExecutingCode, | ||
(isExecutingCode) => !isExecutingCode && resolve(true), | ||
); | ||
}); | ||
} | ||
|
||
const { setIsExecutingCode } = useRunStore.getState(); | ||
setIsExecutingCode(true); | ||
commands.executeCommand("setContext", "SAS.running", true); | ||
const session = getSession(); | ||
|
||
let logText = ""; | ||
const onExecutionLogFn = session.onExecutionLogFn; | ||
const outputLines = []; | ||
const addLine = (logLines: LogLine[]) => | ||
outputLines.push(...logLines.map(({ line }) => line)); | ||
|
||
try { | ||
await session.setup(true); | ||
|
||
// Lets capture output to use it on | ||
session.onExecutionLogFn = addLine; | ||
|
||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
await (session as SASPYSession).run(code); | ||
|
||
const logOutput = outputLines.filter((line) => line.trim()).join(""); | ||
|
||
logText = | ||
startTag && endTag | ||
? logOutput | ||
.slice( | ||
logOutput.lastIndexOf(startTag), | ||
logOutput.lastIndexOf(endTag), | ||
) | ||
.replace(startTag, "") | ||
.replace(endTag, "") | ||
: logOutput; | ||
} finally { | ||
unsubscribe && unsubscribe(); | ||
// Lets update our session to write to the log | ||
session.onExecutionLogFn = onExecutionLogFn; | ||
|
||
setIsExecutingCode(false); | ||
commands.executeCommand("setContext", "SAS.running", false); | ||
} | ||
|
||
return logText; | ||
} | ||
} | ||
|
||
export default CodeRunner; |
Oops, something went wrong.