Skip to content

Commit

Permalink
better insertion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Apr 1, 2023
1 parent d955158 commit 06ed399
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions vscode/src/state.ts
@@ -1,4 +1,8 @@
import type { DeviceConfig, ServerInfoFile } from "@devicescript/interop"
import type {
DeviceConfig,
ServerInfo,
ServerInfoFile,
} from "@devicescript/interop"
import { normalizeDeviceConfig, parseAnyInt } from "@devicescript/interop"
import {
CHANGE,
Expand Down Expand Up @@ -377,10 +381,37 @@ export class DeviceScriptExtensionState extends JDEventSource {
)) {
await this.addImport(editor, symName, modName)
}
await editor.insertSnippet(
new vscode.SnippetString(server.entry.snippet)

await this.addStartServer(editor, server)
}
}

// find first line that is not an import, comment or empty
private async addStartServer(
editor: vscode.TextEditor,
server: {
label: string
description: string
detail: string
entry: ServerInfo
}
) {
const document = editor.document
let line: vscode.TextLine
let i = 0
for (let i = 0; i < document.lineCount; ++i) {
line = document.lineAt(i)
if (
!line.isEmptyOrWhitespace &&
!/^\s*(\/\/|import\s+)/m.test(line.text)
)
break
}
const position = line?.rangeIncludingLineBreak?.start
await editor.insertSnippet(
new vscode.SnippetString(server.entry.snippet),
position
)
}

async addImport(
Expand Down

0 comments on commit 06ed399

Please sign in to comment.