1namespace ts.projectSystem { 2 function setup(fileName: string, content: string) { 3 const file: File = { path: fileName, content }; 4 const host = createServerHost([file, libFile]); 5 const session = createSession(host); 6 openFilesForSession([file], session); 7 return function getSmartSelectionRange(locations: protocol.SelectionRangeRequestArgs["locations"]) { 8 return executeSessionRequest<protocol.SelectionRangeRequest, protocol.SelectionRangeResponse>( 9 session, 10 CommandNames.SelectionRange, 11 { file: fileName, locations }); 12 }; 13 } 14 15 // More tests in fourslash/smartSelection_* 16 describe("unittests:: tsserver:: smartSelection", () => { 17 it("works for simple JavaScript", () => { 18 const getSmartSelectionRange = setup("/file.js", ` 19class Foo { 20 bar(a, b) { 21 if (a === b) { 22 return true; 23 } 24 return false; 25 } 26}`); 27 28 const locations = getSmartSelectionRange([ 29 { line: 4, offset: 13 }, // a === b 30 ]); 31 32 assert.deepEqual(locations, [{ 33 textSpan: { // a 34 start: { line: 4, offset: 13 }, 35 end: { line: 4, offset: 14 } }, 36 parent: { 37 textSpan: { // a === b 38 start: { line: 4, offset: 13 }, 39 end: { line: 4, offset: 20 } }, 40 parent: { 41 textSpan: { // IfStatement 42 start: { line: 4, offset: 9 }, 43 end: { line: 6, offset: 10 } }, 44 parent: { 45 textSpan: { // SyntaxList + whitespace (body of method) 46 start: { line: 3, offset: 16 }, 47 end: { line: 8, offset: 5 } 48 }, 49 parent: { 50 textSpan: { // {} 51 start: { line: 3, offset: 15 }, 52 end: { line: 8, offset: 6 } }, 53 parent: { 54 textSpan: { // MethodDeclaration 55 start: { line: 3, offset: 5 }, 56 end: { line: 8, offset: 6 } }, 57 parent: { 58 textSpan: { // SyntaxList + whitespace (body of class) 59 start: { line: 2, offset: 12 }, 60 end: { line: 9, offset: 1 } }, 61 parent: { 62 textSpan: { // ClassDeclaration 63 start: { line: 2, offset: 1 }, 64 end: { line: 9, offset: 2 } }, 65 parent: { 66 textSpan: { // SourceFile (all text) 67 start: { line: 1, offset: 1 }, 68 end: { line: 9, offset: 2 } } } } } } } } } } }]); 69 }); 70 }); 71} 72