"""Run only in an isolated Sublime Text: copy to its Data/Packages/User/.""" import json import os import xml.etree.ElementTree as ET import sublime SOURCE = '''// struct Fake @State /* @Component struct Hidden {} */ @Entry @Component struct Demo { @State label: string = 'struct @Entry' build() { Column() { Text(`value ${this.label} ${JSON.stringify({ value: 1 })}`) .onClick(() => { this.label = "ok" }) ForEach([1, 2], (item: number) => { Text(`${item}`) }) } .width('100%') } } @ComponentV2 struct Next { @Local count: number = 0 } const escaped = 'can\\'t' const after = 42 const incomplete = 'oops const recovered = 123 const matcher = /[a/b]+\\d{2}/gi const divide = 12 / 3 / 2 const arrow = () => /hello/.test('hello') function pattern(): RegExp { return /world/i } type Nested = Map>> class Service { public async load( value: Array> ): Promise { return value[0].get('key')! } } interface Contract { save(value: string): void; } ''' def plugin_loaded(): sublime.set_timeout(run_checks, 1500) def run_checks(): result = {"build": sublime.version(), "checks": [], "errors": []} view = None try: window = sublime.active_window() view = window.new_file() view.set_scratch(True) settings = view.settings() editor_preferences = {key: settings.get(key) for key in ( 'tab_size', 'translate_tabs_to_spaces', 'auto_indent', 'smart_indent', 'indent_to_bracket', 'trim_automatic_white_space', )} view.assign_syntax('Packages/ArkTS/ArkTS.sublime-syntax') for key, value in editor_preferences.items(): assert settings.get(key) == value, 'ArkTS overrides {}'.format(key) result['checks'].append('ArkTS inherits editor preferences') view.run_command('append', {'characters': SOURCE}) cases = [ ('struct Fake', 'comment.line'), ('@Component struct Hidden', 'comment.block'), ('@Entry\n', 'storage.type.annotation', 1), ('struct Demo', 'storage.type'), ('Demo {', 'entity.name.type'), ("struct @Entry'", 'string.quoted.single'), ('Column()', 'support.function.component'), ('onClick', 'variable.function'), ('this.label}', 'variable.language'), ('JSON.stringify', 'support.type'), ('value: 1', 'variable.other'), ('ForEach', 'support.function.component'), ('width(', 'variable.function'), ('ComponentV2', 'storage.type.annotation'), ('Local count', 'storage.type.annotation'), ('42\n', 'constant.numeric'), ('123\n', 'constant.numeric'), ('[a/b]', 'string.regexp'), ('hello/', 'string.regexp'), ('world/', 'string.regexp'), ('load', 'entity.name.function'), ('Nested =', 'entity.name.type'), ('Map 2 else 0) actual = view.scope_name(point) assert scope in actual, '{}: expected {}, got {}'.format(token, scope, actual) result['checks'].append(token) for token in ('this.label}', 'value: 1', '123\n'): actual = view.scope_name(SOURCE.index(token)) assert 'string.' not in actual, '{} leaked string scope: {}'.format(token, actual) for point in range(view.size()): assert 'invalid.' not in view.scope_name(point), 'Invalid scope at {}'.format(point) result['checks'].append('nested brackets and interpolation recover') slash = SOURCE.index('12 / 3') + 3 assert 'keyword.operator.arkts' in view.scope_name(slash) assert 'string.regexp' not in view.scope_name(slash) result['checks'].append('division is not a regex') names = [name for region, name in view.symbols()] for name in ('Demo', 'Next', 'build', 'pattern', 'Nested', 'Service', 'load', 'Contract', 'save'): assert name in names, 'Missing symbol {} in {}'.format(name, names) for name in ('Column', 'Text', 'ForEach', 'JSON', 'Map', 'Promise', 'Fake', 'Hidden'): assert name not in names, 'Reference leaked into symbols: {}'.format(name, names) result['checks'].append('declaration-only symbol navigation') view.sel().clear() point = SOURCE.index(' @State') view.sel().add(sublime.Region(point, point)) view.run_command('toggle_comment') assert view.substr(view.line(point)).lstrip().startswith('//') result['checks'].append('toggle line comment') view.run_command('toggle_comment') view.sel().clear() view.sel().add(sublime.Region(SOURCE.index('struct Demo'))) view.run_command('fold') assert view.folded_regions(), 'Native folding did not produce a region' result['checks'].append('native folding') view.run_command('unfold_all') settings.set('auto_indent', True) settings.set('smart_indent', True) settings.set('tab_size', 4) for use_spaces, indent in ((True, ' '), (False, '\t')): settings.set('translate_tabs_to_spaces', use_spaces) view.run_command('select_all') view.run_command('insert', {'characters': 'Column() {'}) view.run_command('insert', {'characters': '\n'}) assert view.substr(sublime.Region(0, view.size())) == 'Column() {\n' + indent result['checks'].append('newline indentation: ' + ('four spaces' if use_spaces else 'tab')) snippets = sublime.find_resources('*.sublime-snippet') snippets = [path for path in snippets if path.startswith('Packages/ArkTS/')] assert len(snippets) == 11, 'Expected 11 installed snippets' for path in snippets: body = ET.fromstring(sublime.load_resource(path)).findtext('content') view.run_command('select_all') view.run_command('insert_snippet', {'contents': body}) expanded = view.substr(sublime.Region(0, view.size())) assert expanded and '${' not in expanded and '$0' not in expanded, path result['checks'].append('snippet: ' + path.rsplit('/', 1)[-1]) window.run_command('edit_settings', { 'base_file': '${packages}/ArkTS/ArkTS.sublime-settings' }) assert window.num_groups() == 2, 'Settings should open in split view' result['checks'].append('settings open in split view') except Exception as error: result['errors'].append(str(error)) finally: output = os.path.join(os.path.dirname(sublime.packages_path()), 'arkts-test-results.json') with open(output, 'w', encoding='utf-8') as stream: json.dump(result, stream, indent=2) if view is not None: view.close() sublime.set_timeout(lambda: sublime.run_command('exit'), 500)