Skip to content

Commit

Permalink
fix: document handling (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobyhallx authored Nov 29, 2023
1 parent 78e529a commit f3d2a54
Showing 1 changed file with 72 additions and 66 deletions.
138 changes: 72 additions & 66 deletions src/EditorLineDecorationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,73 +33,77 @@ export class EditorLineDecorationManager extends Disposable {
window.onDidChangeActiveTextEditor((editor) => {
this.displayAllTextDecorations();
});

}

displayAllTextDecorations() {
const document = window.activeTextEditor.document;

let workspaceFolder = workspace
.getWorkspaceFolder(document.uri)
.uri.toString();

const activeClient = this.lspClients.get(workspaceFolder);

// find file which we want to present hints for
let [fileIdKey, _] = (
Object.entries(activeClient.profileRunResult.file_map)
).find(([fileId, fileElement]) => {
return fileElement.path === document.uri.path;
}) as [string, FileInfo];

const fileId = Number(fileIdKey);

// Filter counts for file of interest
const filteredResults = activeClient.profileRunResult.opcodes_counts.filter(
([spanInfo, _]) => {
return spanInfo.file === fileId;
}
);

// Sum Opcodes for lines
const lineAccumulatedOpcodes = filteredResults
.map(([spanInfo, countInfo]) => {
const startPosition = document.positionAt(spanInfo.span.start);
const endPosition = document.positionAt(spanInfo.span.end);

const range = new Range(startPosition, endPosition);

return { range, countInfo };
})
// Lets accumulate ranges by line numbers
.reduce((accumulator, { range, countInfo }) => {
let lineInfo = accumulator[range.end.line];
if (!lineInfo) {
lineInfo = { ranges: [] };
}
lineInfo.ranges.push({ range, countInfo });
accumulator[range.end.line] = lineInfo;
return accumulator;
}, {});

// Count opcodes per line in accumulated collection
(Object.entries(lineAccumulatedOpcodes) as [number, any]).forEach(
([lineNumber, lineInfo]) => {
lineInfo.lineOpcodes = lineInfo.ranges.reduce(
({ acir_size, brillig_size }, { range, countInfo }) => {
acir_size = acir_size + countInfo.acir_size;
brillig_size = brillig_size + countInfo.brillig_size;
return { acir_size, brillig_size };
},
{ acir_size: 0, brillig_size: 0 }
const document = window?.activeTextEditor?.document;

if (document) {
let workspaceFolder = workspace
.getWorkspaceFolder(document.uri)
.uri.toString();

const activeClient = this.lspClients.get(workspaceFolder);

if (activeClient?.profileRunResult) {
// find file which we want to present hints for
let [fileIdKey, _] = Object.entries(
activeClient.profileRunResult.file_map
).find(([fileId, fileElement]) => {
return fileElement.path === document.uri.path;
}) as [string, FileInfo];

const fileId = Number(fileIdKey);

// Filter counts for file of interest
const filteredResults =
activeClient.profileRunResult.opcodes_counts.filter(
([spanInfo, _]) => {
return spanInfo.file === fileId;
}
);

// Sum Opcodes for lines
const lineAccumulatedOpcodes = filteredResults
.map(([spanInfo, countInfo]) => {
const startPosition = document.positionAt(spanInfo.span.start);
const endPosition = document.positionAt(spanInfo.span.end);

const range = new Range(startPosition, endPosition);

return { range, countInfo };
})
// Lets accumulate ranges by line numbers
.reduce((accumulator, { range, countInfo }) => {
let lineInfo = accumulator[range.end.line];
if (!lineInfo) {
lineInfo = { ranges: [] };
}
lineInfo.ranges.push({ range, countInfo });
accumulator[range.end.line] = lineInfo;
return accumulator;
}, {});

// Count opcodes per line in accumulated collection
(Object.entries(lineAccumulatedOpcodes) as [number, any]).forEach(
([lineNumber, lineInfo]) => {
lineInfo.lineOpcodes = lineInfo.ranges.reduce(
({ acir_size, brillig_size }, { range, countInfo }) => {
acir_size = acir_size + countInfo.acir_size;
brillig_size = brillig_size + countInfo.brillig_size;
return { acir_size, brillig_size };
},
{ acir_size: 0, brillig_size: 0 }
);
}
);
}
);

updateDecorations(document, lineAccumulatedOpcodes);

// Used to show Hide Commands in Command Pallette
commands.executeCommand('setContext', 'noir.profileInfoPresent', true);
updateDecorations(document, lineAccumulatedOpcodes);

// Used to show Hide Commands in Command Pallette
commands.executeCommand("setContext", "noir.profileInfoPresent", true);
}
}
}

// Remove all decorations including onHover ones
Expand Down Expand Up @@ -137,20 +141,22 @@ function updateDecorations(document, lineAccumulatedOpcodes: object) {
})
.flat();

window.activeTextEditor.setDecorations(decoration, decorations);
window?.activeTextEditor?.setDecorations(decoration, decorations);
}

function lineDecorator(document: any, lineNumber: string, lineInfo: any) {
const range: Range = document.lineAt(Number(lineNumber)).range;
const lineContent = `// ${
lineInfo.lineOpcodes.acir_size ? `${lineInfo.lineOpcodes.acir_size} ACIR` : ""
lineInfo.lineOpcodes.acir_size
? `${lineInfo.lineOpcodes.acir_size} ACIR`
: ""
} ${lineInfo.brillig_size ? `${lineInfo.brillig_size} ACIR` : ""} opcodes`;
const decorator = {
range,
renderOptions: {
after: {
backgroundColor: new ThemeColor('editorInlayHint.background' ),
color: new ThemeColor('editorInlayHint.foreground' ),
backgroundColor: new ThemeColor("editorInlayHint.background"),
color: new ThemeColor("editorInlayHint.foreground"),
contentText: lineContent,
fontWeight: "normal",
fontStyle: "normal",
Expand Down

0 comments on commit f3d2a54

Please sign in to comment.