mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
|
|
|
|
import sortByEndLineNumberAsc from './sortByEndLineNumberAsc';
|
|
|
|
export default function addMainContractCodeDecoration(model: monaco.editor.ITextModel, contractName: string, editor: monaco.editor.IStandaloneCodeEditor) {
|
|
const options: monaco.editor.IModelDecorationOptions = {
|
|
isWholeLine: true,
|
|
};
|
|
|
|
const contractBlockMatches = model.findMatches(`^contract\\s`, false, true, false, null, true);
|
|
|
|
if (contractBlockMatches.length < 2) {
|
|
return;
|
|
}
|
|
|
|
const [ firstLineMatch ] = model.findMatches(`(^contract ${ contractName })( is .+)?\\s?\\{`, false, true, false, null, true);
|
|
|
|
if (!firstLineMatch) {
|
|
return;
|
|
}
|
|
|
|
const firstLineDecoration: monaco.editor.IModelDeltaDecoration = {
|
|
range: {
|
|
startColumn: 1,
|
|
endColumn: 10, // doesn't really matter since isWholeLine is true
|
|
startLineNumber: firstLineMatch.range.startLineNumber,
|
|
endLineNumber: firstLineMatch.range.startLineNumber,
|
|
},
|
|
options: {
|
|
...options,
|
|
className: '.main-contract-header',
|
|
marginClassName: '.main-contract-header',
|
|
glyphMarginClassName: '.main-contract-glyph',
|
|
glyphMarginHoverMessage: [
|
|
{ value: 'Main contract' },
|
|
],
|
|
},
|
|
};
|
|
|
|
const lastLineRange: monaco.IRange = {
|
|
startLineNumber: firstLineMatch.range.startLineNumber,
|
|
startColumn: 1,
|
|
endColumn: 10,
|
|
endLineNumber: model.getLineCount(),
|
|
};
|
|
const [ lastLineMatch ] = model
|
|
.findMatches(`^\\}`, lastLineRange, true, false, null, true)
|
|
.sort(sortByEndLineNumberAsc);
|
|
|
|
const restDecoration: monaco.editor.IModelDeltaDecoration = {
|
|
range: {
|
|
startLineNumber: firstLineMatch.range.startLineNumber + 1,
|
|
endLineNumber: lastLineMatch.range.startLineNumber,
|
|
startColumn: 1,
|
|
endColumn: 10, // doesn't really matter since isWholeLine is true
|
|
},
|
|
options: {
|
|
...options,
|
|
className: '.main-contract-body',
|
|
marginClassName: '.main-contract-body',
|
|
},
|
|
};
|
|
|
|
editor.updateOptions({ glyphMargin: true });
|
|
model.deltaDecorations([], [ firstLineDecoration, restDecoration ]);
|
|
}
|