-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from jvalue/text-range-selector
Add TextRangeSelector block type
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
libs/extensions/std/exec/src/text-range-selector-executor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as R from '@jvalue/execution'; | ||
import { | ||
BlockExecutor, | ||
BlockExecutorClass, | ||
ExecutionContext, | ||
TextFile, | ||
implementsStatic, | ||
} from '@jvalue/execution'; | ||
import { IOType } from '@jvalue/language-server'; | ||
|
||
@implementsStatic<BlockExecutorClass>() | ||
export class TextRangeSelectorExecutor | ||
implements BlockExecutor<IOType.TEXT_FILE, IOType.TEXT_FILE> | ||
{ | ||
public static readonly type = 'TextRangeSelector'; | ||
public readonly inputType = IOType.TEXT_FILE; | ||
public readonly outputType = IOType.TEXT_FILE; | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async execute( | ||
file: TextFile, | ||
context: ExecutionContext, | ||
): Promise<R.Result<TextFile>> { | ||
const lineFrom = context.getNumericPropertyValue('lineFrom'); | ||
const lineTo = context.getNumericPropertyValue('lineTo'); | ||
|
||
const numberOfLines = file.content.length; | ||
|
||
context.logger.logDebug( | ||
`Selecting lines from ${lineFrom} to ${ | ||
lineTo === Number.POSITIVE_INFINITY || lineTo >= numberOfLines | ||
? 'the end' | ||
: `${lineTo}` | ||
}`, | ||
); | ||
const selectedLines = file.content.slice(lineFrom - 1, lineTo); | ||
|
||
return R.ok( | ||
new TextFile(file.name, file.extension, file.mimeType, selectedLines), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
libs/extensions/std/lang/src/text-range-selector-meta-inf.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { strict as assert } from 'assert'; | ||
|
||
import { | ||
BlockMetaInformation, | ||
IOType, | ||
PropertyAssignment, | ||
PropertyValuetype, | ||
isNumericLiteral, | ||
} from '@jvalue/language-server'; | ||
import { ValidationAcceptor } from 'langium'; | ||
|
||
export class TextRangeSelectorMetaInformation extends BlockMetaInformation { | ||
constructor() { | ||
super( | ||
'TextRangeSelector', | ||
{ | ||
lineFrom: { | ||
type: PropertyValuetype.INTEGER, | ||
defaultValue: 1, | ||
validation: greaterThanZeroValidation, | ||
}, | ||
lineTo: { | ||
type: PropertyValuetype.INTEGER, | ||
defaultValue: Number.POSITIVE_INFINITY, | ||
validation: greaterThanZeroValidation, | ||
}, | ||
}, | ||
// Input type: | ||
IOType.TEXT_FILE, | ||
|
||
// Output type: | ||
IOType.TEXT_FILE, | ||
); | ||
this.docs.description = 'Selects a range of lines from a `TextFile`.'; | ||
} | ||
} | ||
|
||
function greaterThanZeroValidation( | ||
property: PropertyAssignment, | ||
accept: ValidationAcceptor, | ||
) { | ||
const propertyValue = property.value; | ||
assert(isNumericLiteral(propertyValue)); | ||
|
||
if (propertyValue.value <= 0) { | ||
accept('error', `Line numbers need to be greater than zero`, { | ||
node: propertyValue, | ||
}); | ||
} | ||
} |