Skip to content

Commit

Permalink
Fix plugin integration with lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Oct 17, 2024
1 parent 4844582 commit b0dddf0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
1 change: 0 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"name": "Debug test-project",
"type": "brightscript",
"request": "launch",
"host": "${promptForHost}",
"rootDir": "${workspaceFolder}/out/dist",
"preLaunchTask": "build-test-app",
"enableDebugProtocol": true
Expand Down
8 changes: 5 additions & 3 deletions lib/components/Reftracker.bs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "pkg:/source/roku_modules/promises/promises.brs"
import "pkg:/source/reftrackerLib.bs"
import "pkg:/source/roku_modules/promises/promises.brs"

' typecast m as ReftrackerM
interface ReftrackerM
Expand All @@ -19,18 +19,20 @@ function init()
m.keypathsByNodeReftrackerId = {}
'a flat list of all nodes discovered in this run
m.allNodes = []
'a queue of nodes to process
m.nodeQueue = []

m.top.runId = reftracker.internal.getRandomUUID()
end function

function discover()
function discover(_ = invalid)
'seed the list of nodes with all roots (should be a good starting point)
for each root in m.top.getRoots() as roSGnode[]
registerNodeRef(`root:<${root.subtype()}>`, root)
end for

'process the nodes one-by-one
promises.onThen(processNextNode(), function(result)
return promises.onThen(processNextNode(), function(result)
print "done processing nodes"
end function)
end function
Expand Down
16 changes: 11 additions & 5 deletions lib/source/reftrackerLib.bs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace reftracker
'store a reference to it so it doesn't get lost
reftracker.internal.registerReftracker(tracker)
'run it
tracker@.discover()
return tracker@.discover()
end function
end namespace

Expand Down Expand Up @@ -103,18 +103,24 @@ namespace reftracker.internal
return m.processedItems[id] = invalid
end function

'Ensure this node has a unique reftracker id so we can use it for lookups and comparisons
'Ensure this node has a unique reftracker id so we can use it for lookups and equality checks
function getReftrackerId(item as dynamic)
reftrackerId = reftracker.internal.getRandomUUID()
key = "reftracker_id"
itemType = type(item)
if itemType = "roSGNode" and item[key] = invalid then
item.addField(key, "string", reftracker.internal.getRandomUUID())
item.addField(key, "string", false)
item[key] = reftrackerId
else if itemType = "roAssociativeArray" and item[key] = invalid
item[key] = reftracker.internal.getRandomUUID()
item[key] = reftrackerId
else if itemType = "roArray" and item[(item as roArray).Count() - 2] <> "reftracker_id"
item.push("reftracker_id")
item.push(reftracker.internal.getRandomUUID())
item.push(reftrackerId)
else
return invalid
end if

return reftrackerId
end function

function registerReftracker(finder as roSGNodeRefTracker)
Expand Down
45 changes: 31 additions & 14 deletions src/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fsExtra from 'fs-extra';
import * as fastGlob from 'fast-glob';
import type { BeforeFileTranspileEvent, BscFile, CompilerPlugin, Editor, Program, XmlFile } from 'brighterscript';
import { AstEditor, createSGAttribute, isBrsFile, isXmlFile, standardizePath as s } from 'brighterscript';
import type { BscFile, CompilerPlugin, Editor, Program, XmlFile } from 'brighterscript';
import { AstEditor, createSGAttribute, isXmlFile, standardizePath as s, util } from 'brighterscript';
import { SGField, SGFunction, SGInterface, SGScript } from 'brighterscript/dist/parser/SGTypes';
const cwd = s`${__dirname}/../`;

Expand All @@ -10,15 +10,20 @@ export class Plugin implements CompilerPlugin {

afterProgramCreate(program: Program) {
//inject the the required library files at the start of the program so they're available to all files from the very start
const files = fastGlob.sync('**/*.{bs,brs,xml}', {
absolute: false,
cwd: s`${cwd}/lib`
});
const files = fastGlob
.sync('**/*.{bs,brs,xml}', {
absolute: false,
cwd: s`${cwd}/lib`
}).map(x => ({
src: s`${cwd}/lib/${x}`,
dest: x
}));

//add these to the program's files array so it works in lsp mode
program.options.files.push(...files);

for (const file of files) {
program.setFile({
src: s`${cwd}/lib/${file}`,
dest: file
}, fsExtra.readFileSync(s`${cwd}/lib/${file}`).toString());
program.setFile(file, fsExtra.readFileSync(s`${cwd}/lib/${file}`).toString());
}
}

Expand All @@ -29,6 +34,10 @@ export class Plugin implements CompilerPlugin {
}
}

beforeProgramValidate(program: Program) {
console.log('beforeProgramValidate');
}

// beforeFileTranspile(event: BeforeFileTranspileEvent) {
// if (isBrsFile(event.file)) {

Expand All @@ -39,7 +48,6 @@ export class Plugin implements CompilerPlugin {
// }

private injectScriptAndCallfunc(file: XmlFile, editor: Editor) {
//inject the script tag
if (!file.ast.component?.api) {
file.ast.component!.api = new SGInterface();
}
Expand All @@ -51,7 +59,7 @@ export class Plugin implements CompilerPlugin {
editor.arrayPush(
file.ast.component!.api.functions,
new SGFunction(
{ text: 'function' },
{ text: 'function', range: util.createRange(0, 0, 0, 99) },
[
createSGAttribute('name', 'reftracker_internal_execute')
]
Expand All @@ -61,7 +69,7 @@ export class Plugin implements CompilerPlugin {
editor.arrayPush(
file.ast.component!.api.fields,
new SGField(
{ text: 'field' },
{ text: 'field', range: util.createRange(0, 0, 0, 99) },
[
createSGAttribute('id', 'reftrackerEnabled'),
createSGAttribute('type', 'boolean'),
Expand All @@ -74,12 +82,21 @@ export class Plugin implements CompilerPlugin {
editor.arrayPush(
file.ast.component!.scripts,
new SGScript(
{ text: 'script' },
{ text: 'script', range: util.createRange(0, 0, 0, 99) },
[
createSGAttribute('type', 'text/brightscript'),
createSGAttribute('uri', 'pkg:/source/reftrackerLib.bs')
]
)
);
// //for bsc v0, we also have to manipulate parser.references.scriptTagImports
// editor.arrayPush(
// file.parser.references.scriptTagImports,
// {
// pkgPath: s`source/reftrackerLib.bs`,
// text: 'pkg:/source/reftrackerLib.bs',
// filePathRange: util.createRange(0, 0, 0, 0)
// }
// );
}
}
1 change: 1 addition & 0 deletions test-project/bsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"rootDir": "./",
"createPackage": false,
"stagingDir": "../out/dist",
"retainStagingDir": true,
Expand Down
4 changes: 3 additions & 1 deletion test-project/components/MainScene.bs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ sub init()
'make the text really big
m.mainLabel.font.size = 100

reftracker.findNodeById("bob")
promises.chain(reftracker.findNodeById("bob")).then(function(result, node)
print "found node"
end function)
end sub

0 comments on commit b0dddf0

Please sign in to comment.