1+ /// <reference path="../node_modules/@types/jasmine/index.d.ts" />
2+
3+ import * as ts from 'typescript' ;
4+ import { LineIndexSnapshot , Logger , ProjectService , ProjectServiceHost , ScriptInfo } from '../src/editorServices' ;
5+
6+ import * as u from './test_utils' ;
7+ import { QUICKSTART } from './test_data' ;
8+
9+ describe ( 'editor services' , ( ) => {
10+ let projectService : ProjectService ;
11+ beforeAll ( ( ) => {
12+ projectService = new ProjectService ( new u . MockProjectServiceHost ( QUICKSTART ) , new u . MockLogger ( ) ) ;
13+ } ) ;
14+
15+ it ( 'should be able to get the information for a file' , ( ) => {
16+ projectService . openClientFile ( '/app/app.component.ts' ) ;
17+ const info = projectService . getScriptInfo ( '/app/app.component.ts' ) ;
18+ expect ( info ) . not . toBeUndefined ( ) ;
19+ } ) ;
20+
21+ describe ( 'file' , ( ) => {
22+ function tests ( fileName : string , content : string ) {
23+ let info : ScriptInfo ;
24+ let snap : LineIndexSnapshot ;
25+ let len : number ;
26+
27+ beforeEach ( ( ) => {
28+ projectService . openClientFile ( fileName , content ) ;
29+ info = projectService . getScriptInfo ( fileName ) ;
30+ snap = info . snap ( ) ;
31+ len = snap . getLength ( ) ;
32+ } )
33+
34+ it ( 'should be able to get pieces of a file' , ( ) => {
35+ const firstText = snap . getText ( 0 , len ) ;
36+ for ( let i = 0 ; i < len ; i ++ ) {
37+ expect ( snap . getText ( i , i + 1 ) ) . toEqual ( firstText [ i ] ) ;
38+ }
39+ } ) ;
40+
41+ it ( 'should be able to modify the file and get the expected content' , ( ) => {
42+ const offsetOfComponent = content . indexOf ( '@Component' ) ;
43+ projectService . clientFileChanges ( fileName , [ { start : offsetOfComponent , end : offsetOfComponent , insertText : ' ' } ] ) ;
44+ const text = info . getText ( ) ;
45+ expect ( text ) . toEqual ( content . replace ( '@Component' , ' @Component' ) ) ;
46+ } ) ;
47+
48+ describe ( 'and line starts' , ( ) => {
49+ let lineStarts : number [ ] ;
50+
51+ beforeAll ( ( ) => {
52+ lineStarts = getLineStarts ( content ) ;
53+ } ) ;
54+
55+ it ( 'should be able to get the expected line columns' , ( ) => {
56+ for ( let i = 0 ; i < len ; i ++ ) {
57+ const expected = lineColOf ( i , lineStarts ) ;
58+ const result = projectService . positionsToLineOffsets ( fileName , [ i ] ) ;
59+ expect ( result ) . toEqual ( [ expected ] ) ;
60+ }
61+ } ) ;
62+
63+ it ( 'should be able to turn line columns in offsets' , ( ) => {
64+ for ( let i = 0 ; i < len ; i ++ ) {
65+ const expected = lineColOf ( i , lineStarts ) ;
66+ const result = projectService . lineOffsetsToPositions ( fileName , [ expected ] ) ;
67+ expect ( result ) . toEqual ( [ i ] ) ;
68+ }
69+ } ) ;
70+ } ) ;
71+ }
72+
73+ const fileName = '/app/app.component.ts' ;
74+ describe ( 'with LF' , ( ) => {
75+ tests ( fileName , u . read ( fileName , QUICKSTART ) ) ;
76+ } ) ;
77+ describe ( 'with LF/CR' , ( ) => {
78+ tests ( fileName , u . read ( fileName , QUICKSTART ) . replace ( / \n / g, '\r\n' ) ) ;
79+ } ) ;
80+ } ) ;
81+ } ) ;
82+
83+ function getLineStarts ( content : string ) : number [ ] {
84+ const result = [ 0 ] ;
85+ const len = content . length ;
86+ for ( let i = 0 ; i < len ; i ++ ) {
87+ if ( content [ i ] == `\n` ) result . push ( i + 1 ) ;
88+ }
89+ result . push ( len ) ;
90+ return result ;
91+ }
92+
93+ function lineColOf ( position : number , lineStarts : number [ ] ) {
94+ for ( let line = 0 ; line < lineStarts . length ; line ++ ) {
95+ if ( lineStarts [ line + 1 ] > position ) {
96+ return { line : line + 1 , col : position - lineStarts [ line ] + 1 } ;
97+ }
98+ }
99+ }
0 commit comments