Skip to content

Commit

Permalink
fix: incorrect parsing of stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon committed Jan 28, 2024
1 parent 330fd89 commit 28846be
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ This was made to run with [Bun](https://bun.sh/), but you can also use a precomp
#### Without Bun

```bash
wget https://github.com/leona/helix-gpt/releases/download/0.14/helix-gpt-0.14-x86_64-linux.tar.gz \
wget https://github.com/leona/helix-gpt/releases/download/0.15/helix-gpt-0.15-x86_64-linux.tar.gz \
-O /tmp/helix-gpt.tar.gz \
&& tar -zxvf /tmp/helix-gpt.tar.gz \
&& mv helix-gpt-0.14-x86_64-linux /usr/bin/helix-gpt \
&& mv helix-gpt-0.15-x86_64-linux /usr/bin/helix-gpt \
&& chmod +x /usr/bin/helix-gpt
```

#### With Bun

```bash
wget https://github.com/leona/helix-gpt/releases/download/0.14/helix-gpt-0.14.js -O helix-gpt.js
wget https://github.com/leona/helix-gpt/releases/download/0.15/helix-gpt-0.15.js -O helix-gpt.js
```

### Configuration
Expand Down
2 changes: 1 addition & 1 deletion src/events/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const actions = (lsp: IService) => {
const buffer = ctx.buffers[ctx.currentUri]

try {
var result = await chatHandler(query, content, ctx.currentUri as string, buffer.languageId as string)
var result = await chatHandler(query, content, ctx.currentUri as string, buffer?.languageId as string)

if (!result?.length) {
throw new Error("No completion found")
Expand Down
2 changes: 1 addition & 1 deletion src/events/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const completions = (lsp: IService) => {
], 10000)

try {
var hints = await completionHandler({ contentBefore, contentAfter }, ctx.currentUri, buffer.languageId)
var hints = await completionHandler({ contentBefore, contentAfter }, ctx.currentUri, buffer?.languageId)

} catch (e) {
return ctx.sendDiagnostics([
Expand Down
15 changes: 15 additions & 0 deletions src/models/lsp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from "bun:test";
import Lsp from "./lsp"

test("parseLine", async () => {
const lsp = new Lsp.Service({})

const content1 = `Content-Length: 154\r\n{"id": 1}\r\nContent-Length: 200\n`
expect(lsp.parseLine(content1)).toEqual({ id: 1 })

const content2 = `Content-Length: 1766\r\n{"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{"general":{"positionEncodings":["utf-8","utf-32","utf-16"]},"textDocument":{"codeAction":{"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}},"dataSupport":true,"disabledSupport":true,"isPreferredSupport":true,"resolveSupport":{"properties":["edit","command"]}},"completion":{"completionItem":{"deprecatedSupport":true,"insertReplaceSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"snippetSupport":true,"tagSupport":{"valueSet":[1]}},"completionItemKind":{}},"hover":{"contentFormat":["markdown"]},"inlayHint":{"dynamicRegistration":false},"publishDiagnostics":{"versionSupport":true},"rename":{"dynamicRegistration":false,"honorsChangeAnnotations":false,"prepareSupport":true},"signatureHelp":{"signatureInformation":{"activeParameterSupport":true,"documentationFormat":["markdown"],"parameterInformation":{"labelOffsetSupport":true}}}},"window":{"workDoneProgress":true},"workspace":{"applyEdit":true,"configuration":true,"didChangeConfiguration":{"dynamicRegistration":false},"didChangeWatchedFiles":{"dynamicRegistration":true,"relativePatternSupport":false},"executeCommand":{"dynamicRegistration":false},"inlayHint":{"refreshSupport":false},"symbol":{"dynamicRegistration":false},"workspaceEdit":{"documentChanges":true,"failureHandling":"abort","normalizesLineEndings":false,"resourceOperations":["create","rename","delete"]},"workspaceFolders":true}},"clientInfo":{"name":"helix","version":"23.10"},"processId":4557,"rootPath":"/app","rootUri":"file:///app","workspaceFolders":[{"name":"app","uri":"file:///app"}]},"id":0}`
expect(lsp.parseLine(content2)).toMatchObject({ method: "initialize" })

const content3 = `{"id": 1}\r\nContent-Length: 200\n`
expect(lsp.parseLine(content3)).toEqual({ id: 1 })
})
19 changes: 16 additions & 3 deletions src/models/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class Service {
}

getContentFromRange(range: Range): string {
log("getting content from range", JSON.stringify(range), `uri: ${this.currentUri}`)
log("getting content from range", JSON.stringify(range), `uri: ${this.currentUri}`, `current buffers: ${JSON.stringify(Object.keys(this.buffers))}`)
const { start, end } = range
return this.buffers[this.currentUri].text?.split("\n")?.slice(start.line, end.line + 1).join("\n")
return this.buffers[this.currentUri]?.text?.split("\n")?.slice(start.line, end.line + 1).join("\n")
}

positionalUpdate(uri: string, text: string, range: Range) {
Expand Down Expand Up @@ -155,9 +155,21 @@ class Service {
})
}

parseLine(line: string) {
const components = line.split('\r\n')

for (const data of components) {
try {
return JSON.parse(data)
} catch (e) { }
}

throw new Error("failed to parse")
}

async receiveLine(line: string) {
try {
const request = JSON.parse(line.split('\r\n')[2].split('Content-Length')[0])
const request = this.parseLine(line)

if (![Event.DidChange, Event.DidOpen].includes(request.method)) {
log("received request:", JSON.stringify(request))
Expand All @@ -169,6 +181,7 @@ class Service {
}
}


async start() {
for await (const chunk of Bun.stdin.stream()) {
const chunkText = Buffer.from(chunk).toString();
Expand Down

0 comments on commit 28846be

Please sign in to comment.