Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"vscode": "^1.43.0"
},
"dependencies": {
"adm-zip": "^0.5.10",
"vscode-languageclient": "^8.0.1"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import initialiseState from './services/state'
import * as handlers from './handlers'
import { callResolversAndEmptyList } from './services/timers'
import { registerFlixReleaseDocumentProvider } from './services/releaseVirtualDocument'
import { registerFlixStdlibDocumentProvider } from './services/stdlibVirtualDocument'
import { USER_MESSAGE } from './util/userMessages'
import { StatusCode } from './util/statusCodes'

Expand Down Expand Up @@ -110,6 +111,7 @@ export async function activate(context: vscode.ExtensionContext, launchOptions:
initialiseState(context)

registerFlixReleaseDocumentProvider(context)
registerFlixStdlibDocumentProvider(context)

// create output channels
outputChannel = vscode.window.createOutputChannel('Flix Compiler')
Expand Down
5 changes: 5 additions & 0 deletions client/src/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ export function handleChangeEditor(editor: vscode.TextEditor | undefined) {
return
}

// Skip validation for virtual URIs (like stdlib files from JAR)
if (editor.document.uri.scheme !== 'file') {
return
}

const included = vscode.languages.match({ pattern: FLIX_GLOB_PATTERN }, editor.document)
if (!included) {
vscode.window.showWarningMessage(USER_MESSAGE.FILE_NOT_PART_OF_PROJECT())
Expand Down
76 changes: 76 additions & 0 deletions client/src/services/stdlibVirtualDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2025 Valentin Erokhin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as vscode from 'vscode'
import AdmZip from 'adm-zip'
import * as fs from 'fs'

/**
* The scheme used to distinguish stdlib documents from jar files.
*/
const scheme = 'flixstdlib'

/**
* Handle documents where the uri scheme is [[scheme]] (flixstdlib):
* - Extract the jar path and file path from the URI
* - Read the file content from inside the jar
* - Return the file content for display
*/
const flixStdlibDocumentProvider = new (class implements vscode.TextDocumentContentProvider {
provideTextDocumentContent(uri: vscode.Uri): string {
try {
const jarPath = decodeURIComponent(uri.authority)
const filePath = uri.path.startsWith('/') ? uri.path.substring(1) : uri.path

if (!fs.existsSync(jarPath)) {
return `// Error: Flix jar file not found at: ${jarPath}`
}

const zip = new AdmZip(jarPath)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here your strategy is to extract the source code from the jar.

But an alternative would be this:

We add a new command to VSCodeLspServer called "GetSource(name)".

VSCodeLspServer has access to TypedAst.Root which has a field sources: Map[Source, SourceLocation]

From this we can extract the source code of any file loaded into the compiler by other means than the IDE (e.g. from the std lib or from packages)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is an alternative, if we cannot get VSCode to load a file from inside a zip (maybe thats not yet possible as the ticket seems to suggest??)

let entry = zip.getEntry(filePath)
let searchedPaths = [filePath]

// If not found at root level, try common stdlib directories
if (!entry && !filePath.startsWith('src/')) {
const commonPaths = [
`src/library/${filePath}`,
`src/resources/${filePath}`,
]

for (const path of commonPaths) {
entry = zip.getEntry(path)
searchedPaths.push(path)
if (entry) break
}
}

if (!entry) {
return `// Error: File '${filePath}' not found in jar: ${jarPath}\n// Searched paths: ${searchedPaths.join(', ')}`
}

return entry.getData().toString('utf8')
} catch (error) {
return `// Error reading stdlib file: ${error.message}`
}
}
})()

/**
* Register the stdlib document provider.
*/
export function registerFlixStdlibDocumentProvider({ subscriptions }: vscode.ExtensionContext) {
subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(scheme, flixStdlibDocumentProvider))
}
26 changes: 26 additions & 0 deletions server/src/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ function makeGotoDefinitionResponseHandler(promiseResolver: (result?: socket.Fli
if (status === StatusCode.Success) {
if (targetUri?.startsWith('file://')) {
return promiseResolver(result)
} else if (targetUri && isStdlibFile(targetUri)) {
const flixJarPath = engine.getFlixFilename()
const virtualUri = `flixstdlib://${encodeURIComponent(flixJarPath)}/${targetUri}`

return promiseResolver({
...result,
targetUri: virtualUri
})
} else {
sendNotification(jobs.Request.internalMessage, USER_MESSAGE.FILE_NOT_AVAILABLE(targetUri!))
}
Expand All @@ -216,6 +224,24 @@ function makeGotoDefinitionResponseHandler(promiseResolver: (result?: socket.Fli
}
}

/**
* Determines if a target URI represents a stdlib file that should be loaded from the flix.jar
*/
function isStdlibFile(targetUri: string): boolean {
// Stdlib files are typically:
// 1. Flix source files (end with .flix)
// 2. Just filenames without directory paths (no '/' characters)
// 3. Not absolute paths or URLs
return (
targetUri.endsWith('.flix') &&
!targetUri.includes('/') &&
!targetUri.includes('\\') &&
!targetUri.startsWith('http') &&
!targetUri.includes(':') &&
targetUri.length > 0
)
}

/**
* @function
*/
Expand Down