Skip to content

Commit

Permalink
Merge pull request #233 from jvalue/text-range-selector
Browse files Browse the repository at this point in the history
Add TextRangeSelector block type
  • Loading branch information
felix-oq authored Mar 23, 2023
2 parents e6267ce + bff6d25 commit c7bd566
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/extensions/std/exec/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ArchiveInterpreterExecutor } from './archive-interpreter-executor';
import { FilePickerExecutor } from './file-picker-executor';
import { HttpExtractorExecutor } from './http-extractor-executor';
import { TextFileInterpreterExecutor } from './text-file-interpreter-executor';
import { TextRangeSelectorExecutor } from './text-range-selector-executor';

export class StdExecExtension implements JayveeExecExtension {
private readonly wrappedExtensions: JayveeExecExtension[] = [
Expand All @@ -18,6 +19,7 @@ export class StdExecExtension implements JayveeExecExtension {
...this.wrappedExtensions.map((x) => x.getBlockExecutors()).flat(),
HttpExtractorExecutor,
TextFileInterpreterExecutor,
TextRangeSelectorExecutor,
ArchiveInterpreterExecutor,
FilePickerExecutor,
];
Expand Down
42 changes: 42 additions & 0 deletions libs/extensions/std/exec/src/text-range-selector-executor.ts
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),
);
}
}
2 changes: 2 additions & 0 deletions libs/extensions/std/lang/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ArchiveInterpreterMetaInformation } from './archive-interpreter-meta-in
import { FilePickerMetaInformation } from './file-picker-meta-inf';
import { HttpExtractorMetaInformation } from './http-extractor-meta-inf';
import { TextFileInterpreterMetaInformation } from './text-file-interpreter-meta-inf';
import { TextRangeSelectorMetaInformation } from './text-range-selector-meta-inf';

export class StdLangExtension implements JayveeLangExtension {
private readonly wrappedExtensions: JayveeLangExtension[] = [
Expand All @@ -22,6 +23,7 @@ export class StdLangExtension implements JayveeLangExtension {
...this.wrappedExtensions.map((x) => x.getBlockMetaInf()).flat(),
HttpExtractorMetaInformation,
TextFileInterpreterMetaInformation,
TextRangeSelectorMetaInformation,
ArchiveInterpreterMetaInformation,
FilePickerMetaInformation,
];
Expand Down
50 changes: 50 additions & 0 deletions libs/extensions/std/lang/src/text-range-selector-meta-inf.ts
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,
});
}
}

0 comments on commit c7bd566

Please sign in to comment.