Skip to content

Commit

Permalink
Merge pull request #5 from koliyo:feature/custom-hylo-stdlib-path
Browse files Browse the repository at this point in the history
Add hylo stdlib to VSCode extension
  • Loading branch information
koliyo authored Sep 29, 2023
2 parents fd2e945 + d69d43f commit 9a66d3a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 87 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ jobs:
run: |
set -eo pipefail
BUILD_DIR=$(swift build -c ${{ matrix.configuration }} --show-bin-path)
mkdir -p hylo-vscode-extension/bin/mac/arm64
cp -fv $BUILD_DIR/hylo-lsp-server hylo-vscode-extension/bin/mac/arm64
DIST_DIR=hylo-vscode-extension/dist
mkdir -p $DIST_DIR
rm -rf $DIST_DIR/stdlib
cp -Rp hylo/Library/Hylo $DIST_DIR/stdlib
mkdir -p $DIST_DIR/bin/mac/arm64
cp -fv $BUILD_DIR/hylo-lsp-server $DIST_DIR/bin/mac/arm64
cd hylo-vscode-extension
npm install
npm run vscode:package
Expand All @@ -168,7 +172,7 @@ jobs:
with:
name: vscode-extension-artifacts
path: |
hylo-vscode-extension/hylo-lang-*.vsix
hylo-vscode-extension/*.vsix
dist
!dist/**/*.md
Expand Down
3 changes: 3 additions & 0 deletions Sources/hylo-lsp/AST+FindTranslationUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension AST {
let site = node.site

if node is TranslationUnit {
// logger.debug("[\(site.file.url.absoluteString)] Look for document: \(query)")
if site.file.url.absoluteString == query {
match = TranslationUnit.ID(n)
}
Expand All @@ -32,6 +33,8 @@ extension AST {

public func findTranslationUnit(_ url: DocumentUri) -> TranslationUnit.ID? {
var finder = TranslationUnitFinder(url)

// for m in modules.concatenated(with: [coreLibrary!]) {
for m in modules {
walk(m, notifying: &finder)
if finder.match != nil {
Expand Down
40 changes: 1 addition & 39 deletions Sources/hylo-lsp/AST+ListDocumentSymbols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct DocumentSymbolWalker {

mutating func addDecl(_ id: AnyDeclID) {
let node = ast[id]
logger.debug("Found symbol node: \(id), site: \(node.site)")
// logger.debug("Found symbol node: \(id), site: \(node.site)")
symbols.append(id)
addMembers(id: id, node: node)
}
Expand All @@ -57,44 +57,6 @@ struct DocumentSymbolWalker {
break
}
}


mutating func willEnter(_ n: AnyNodeID, in ast: AST) -> Bool {
let node = ast[n]
let site = node.site

if let scheme = site.file.url.scheme {
if scheme == "synthesized" {
return true
}
else if n.kind == TranslationUnit.self && scheme == "file" {
if site.file.url.absoluteString != document {
// logger.debug("Ignore file: \(site.file.url)")
return false
}
// logger.debug("Enter file: \(site.file.url)")
}
}

if n.kind == NamespaceDecl.self || n.kind == TranslationUnit.self {
return true
}


if n.kind == BindingDecl.self {
return true
}

// if n.kind == FunctionDecl.self || n.kind == VarDecl.self {
if let d = AnyDeclID(n) {
logger.debug("Found symbol node: \(d), site: \(site)")
symbols.append(d)
return false
}

// logger.debug("Ignore node: \(n)")
return true
}
}

extension AST {
Expand Down
140 changes: 98 additions & 42 deletions Sources/hylo-lsp/HyloServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,73 @@ public class LspState {
// self.ast = ast
self.lsp = lsp
self.documents = [:]

if let path = ProcessInfo.processInfo.environment["HYLO_STDLIB_PATH"] {
logger.info("Hylo stdlib filepath from HYLO_STDLIB_PATH: \(path)")
defaultStdlibFilepath = URL(fileURLWithPath: path)
}
else {
defaultStdlibFilepath = HyloModule.standardLibrary
}
}

public func buildStdlibAST() throws -> AST {
return try AST(libraryRoot: HyloModule.standardLibrary)
let defaultStdlibFilepath: URL

public func isStdlibDocument(_ uri: DocumentUri) -> Bool {
let (_, isStdlibDocument) = getStdlibPath(uri)
return isStdlibDocument
}

// var task: Task<TypedProgram, Error>?
func getStdlibPath(_ uri: DocumentUri) -> (stdlibPath: URL, isStdlibDocument: Bool) {
guard let url = URL(string: uri) else {
logger.error("invalid document uri: \(uri)")
return (defaultStdlibFilepath, false)
}

var it = url.deletingLastPathComponent()

// Check if current document is inside a stdlib source directory
while it.path != "/" {
let voidPath = NSString.path(withComponents: [it.path, "Core", "Void.hylo"])
let fm = FileManager.default
var isDirectory: ObjCBool = false
if fm.fileExists(atPath: voidPath, isDirectory: &isDirectory) && !isDirectory.boolValue {
// let relUrl = URL(string: url.absoluteString, relativeTo: it)!
// logger.info("it: \(it), relUrl: \(relUrl.relativePath), path: \(relUrl.path)")
// uri = state.stdlibFilepath.appending(component: relUrl.relativePath).absoluteString
return (it, true)
}

it = it.deletingLastPathComponent()
}

return (defaultStdlibFilepath, false)
}



public func buildProgram(_ doc: DocumentUri) {
let (stdlibPath, isStdlibDocument) = getStdlibPath(doc)

let inputs: [URL] = if !isStdlibDocument { [URL.init(string: doc)!] } else { [] }

public func buildProgram(_ uri: DocumentUri) {
let task = Task {
let inputs = [URL.init(string: uri)!]
return try _buildProgram(inputs)
return try _buildProgram(stdlibPath: stdlibPath, inputs: inputs)
}

logger.debug("Register opened document: \(uri)")
documents[uri] = Document(uri: uri, task: task)
logger.debug("Register opened document: \(doc)")
documents[doc] = Document(uri: doc, task: task)
}

public func _buildProgram(_ inputs: [URL]) throws -> (AST, TypedProgram) {
public func _buildProgram(stdlibPath: URL, inputs: [URL]) throws -> (AST, TypedProgram) {
// let inputs = files.map { URL.init(fileURLWithPath: $0)}
let importBuiltinModule = false
let compileSequentially = false

var diagnostics = DiagnosticSet()
logger.debug("buildProgram: \(inputs)")

var ast = try buildStdlibAST()
var ast = try AST(libraryRoot: stdlibPath)
_ = try ast.makeModule(HyloNotificationHandler.productName, sourceCode: sourceFiles(in: inputs),
builtinModuleAccess: importBuiltinModule, diagnostics: &diagnostics)

Expand Down Expand Up @@ -104,37 +144,19 @@ public struct HyloNotificationHandler : NotificationHandler {

}

func buildDocument(_ uri: DocumentUri) async {
// state.uri = uri
// logger.debug("buildDocument: \(uri)")
// logger.debug("lib uri: \(HyloModule.standardLibrary!.absoluteString)")
// if uri.commonPrefix(with: HyloModule.standardLibrary!.absoluteString) == HyloModule.standardLibrary!.absoluteString {
if uri.contains("hylo/Library/Hylo") {
// logger.debug("document is lib")
state.documents[uri] = Document(uri: uri, ast: state.stdlibAST!, program: state.stdlibProgram!)
}
else {
// logger.debug("document is not lib")
// let inputs = [URL.init(string: uri)!]
// let inputs = [uri]
// let importBuiltinModule = false
// let compileSequentially = false

// var diagnostics = DiagnosticSet()

state.buildProgram(uri)
// withErrorLogging {
// try state.buildProgram(inputs)
// _ = try state.ast.makeModule(HyloNotificationHandler.productName, sourceCode: sourceFiles(in: inputs),
// builtinModuleAccess: importBuiltinModule, diagnostics: &diagnostics)

// state.program = try TypedProgram(
// annotating: ScopedProgram(state.ast), inParallel: !compileSequentially,
// reportingDiagnosticsTo: &diagnostics,
// tracingInferenceIf: nil)
// }
}

func buildDocument(_ uri: DocumentUri) async {
state.buildProgram(uri)
// // Check document is part of stdlib
// var uri = uri
// if isStdlibPath(uri) {
// logger.debug("Document is stdlib member: \(uri)")
// state.documents[uri] = Document(uri: uri, ast: state.stdlibAST!, program: state.stdlibProgram!)
// return
// }
// else {
// state.buildProgram(uri)
// }
}

public func textDocumentDidOpen(_ params: TextDocumentDidOpenParams) async {
Expand Down Expand Up @@ -240,7 +262,8 @@ public struct HyloRequestHandler : RequestHandler {
public func initialize(_ params: InitializeParams) async -> Result<InitializationResponse, AnyJSONRPCResponseError> {

do {
let ast = try state.buildStdlibAST()
// let ast = try state.buildStdlibAST()
let ast = try AST(libraryRoot: state.defaultStdlibFilepath)

var diagnostics = DiagnosticSet()
state.stdlibAST = ast
Expand Down Expand Up @@ -452,14 +475,35 @@ public struct HyloRequestHandler : RequestHandler {
switch id.kind {
case BindingDecl.self: return SymbolKind.field
case VarDecl.self: return SymbolKind.field
case InitializerDecl.self: return SymbolKind.function
case InitializerDecl.self: return SymbolKind.constructor
case FunctionDecl.self: return SymbolKind.function
case SubscriptDecl.self: return SymbolKind.function
case ProductTypeDecl.self: return SymbolKind.struct
case ConformanceDecl.self: return SymbolKind.struct
case TraitDecl.self: return SymbolKind.interface
default: return SymbolKind.object
}
}

func name(of t: AnyType) -> String {
switch t.base {
case let u as ProductType:
return u.name.value
case let u as TypeAliasType:
return u.name.value
case let u as AssociatedTypeType:
return u.name.value
case let u as GenericTypeParameterType:
return u.name.value
case let u as NamespaceType:
return u.name.value
case let u as TraitType:
return u.name.value
default:
fatalError("not implemented")
}
}

public func documentSymbol(_ params: DocumentSymbolParams, _ program: TypedProgram, _ ast: AST) async -> Result<DocumentSymbolResponse, AnyJSONRPCResponseError> {
let symbols = ast.listDocumentSymbols(params.textDocument.uri, program)
if symbols.isEmpty {
Expand Down Expand Up @@ -495,6 +539,18 @@ public struct HyloRequestHandler : RequestHandler {
range: range,
selectionRange: selectionRange
))
case let d as ConformanceDecl:
let sub = ast[d.subject]
let name = if let t = program.exprType[d.subject] { name(of: t) } else { "conformance" }
lspSymbols.append(DocumentSymbol(
name: name,
detail: detail,
kind: kind(s),
range: LSPRange(sub.site),
selectionRange: selectionRange
))


default:
if let name = program.name(of: s) {
lspSymbols.append(DocumentSymbol(
Expand Down
8 changes: 6 additions & 2 deletions build-and-install-vscode-extension.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ set -ex
# . ./setup-env.sh
swift build -c release
BUILD_DIR=$(swift build -c release --show-bin-path)
mkdir -p hylo-vscode-extension/bin/mac/arm64
cp -fv $BUILD_DIR/hylo-lsp-server hylo-vscode-extension/bin/mac/arm64
DIST_DIR=hylo-vscode-extension/dist
mkdir -p $DIST_DIR
rm -rf $DIST_DIR/stdlib
cp -Rp hylo/Library/Hylo $DIST_DIR/stdlib
mkdir -p $DIST_DIR/bin/mac/arm64
cp -fv $BUILD_DIR/hylo-lsp-server $DIST_DIR/bin/mac/arm64
cd hylo-vscode-extension
npm install
npm run vscode:package
Expand Down
2 changes: 1 addition & 1 deletion hylo-vscode-extension

0 comments on commit 9a66d3a

Please sign in to comment.