|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ParseLocation, ParseSourceSpan} from '@angular/compiler'; |
| 10 | +import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core'; |
| 11 | +import {isExternalResource} from '@angular/compiler-cli/src/ngtsc/metadata'; |
| 12 | +import {isNamedClassDeclaration} from '@angular/compiler-cli/src/ngtsc/reflection'; |
| 13 | +import * as t from '@angular/compiler/src/render3/r3_ast'; // t for template AST |
| 14 | +import ts from 'typescript'; |
| 15 | + |
| 16 | +import {getFirstComponentForTemplateFile, isTypeScriptFile, toTextSpan} from './utils'; |
| 17 | + |
| 18 | +export function getOutliningSpans(compiler: NgCompiler, fileName: string): ts.OutliningSpan[] { |
| 19 | + if (isTypeScriptFile(fileName)) { |
| 20 | + const sf = compiler.getCurrentProgram().getSourceFile(fileName); |
| 21 | + if (sf === undefined) { |
| 22 | + return []; |
| 23 | + } |
| 24 | + |
| 25 | + const templatesInFile: Array<t.Node[]> = []; |
| 26 | + for (const stmt of sf.statements) { |
| 27 | + if (isNamedClassDeclaration(stmt)) { |
| 28 | + const resources = compiler.getComponentResources(stmt); |
| 29 | + if (resources === null || isExternalResource(resources.template)) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + const template = compiler.getTemplateTypeChecker().getTemplate(stmt); |
| 33 | + if (template === null) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + templatesInFile.push(template); |
| 37 | + } |
| 38 | + } |
| 39 | + return templatesInFile.map(template => BlockVisitor.getBlockSpans(template)).flat(); |
| 40 | + } else { |
| 41 | + const templateInfo = getFirstComponentForTemplateFile(fileName, compiler); |
| 42 | + if (templateInfo === undefined) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + const {template} = templateInfo; |
| 46 | + return BlockVisitor.getBlockSpans(template); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class BlockVisitor extends t.RecursiveVisitor { |
| 51 | + readonly blocks = [] as |
| 52 | + Array<t.IfBlockBranch|t.ForLoopBlockEmpty|t.ForLoopBlock|t.SwitchBlockCase|t.SwitchBlock| |
| 53 | + t.DeferredBlockError|t.DeferredBlockPlaceholder|t.DeferredBlockLoading>; |
| 54 | + |
| 55 | + static getBlockSpans(templateNodes: t.Node[]): ts.OutliningSpan[] { |
| 56 | + const visitor = new BlockVisitor(); |
| 57 | + t.visitAll(visitor, templateNodes); |
| 58 | + const {blocks} = visitor; |
| 59 | + return blocks.map(block => { |
| 60 | + let mainBlockSpan = block.sourceSpan; |
| 61 | + // The source span of for loops and deferred blocks contain all parts (ForLoopBlockEmpty, |
| 62 | + // DeferredBlockLoading, etc.). The folding range should only include the main block span for |
| 63 | + // these. |
| 64 | + if (block instanceof t.ForLoopBlock || block instanceof t.DeferredBlock) { |
| 65 | + mainBlockSpan = block.mainBlockSpan; |
| 66 | + } |
| 67 | + return { |
| 68 | + // We move the end back 1 character so we do not consume the close brace of the block in the |
| 69 | + // range. |
| 70 | + textSpan: toTextSpan( |
| 71 | + new ParseSourceSpan(block.startSourceSpan.end, mainBlockSpan.end.moveBy(-1))), |
| 72 | + hintSpan: toTextSpan(block.startSourceSpan), |
| 73 | + bannerText: '...', |
| 74 | + autoCollapse: false, |
| 75 | + kind: ts.OutliningSpanKind.Region, |
| 76 | + }; |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + visit(node: t.Node) { |
| 81 | + if (node instanceof t.IfBlockBranch || node instanceof t.ForLoopBlockEmpty || |
| 82 | + node instanceof t.ForLoopBlock || node instanceof t.SwitchBlockCase || |
| 83 | + node instanceof t.SwitchBlock || node instanceof t.DeferredBlockError || |
| 84 | + node instanceof t.DeferredBlockPlaceholder || node instanceof t.DeferredBlockLoading || |
| 85 | + node instanceof t.DeferredBlock) { |
| 86 | + this.blocks.push(node); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments