|
| 1 | +import { afterEach, assert, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { DocumentOnTypeFormattingParams } from 'vscode-languageserver'; |
| 3 | +import { Position } from 'vscode-languageserver-protocol'; |
| 4 | +import { TextDocument } from 'vscode-languageserver-textdocument'; |
| 5 | +import { DocumentManager } from '../../documents'; |
| 6 | +import { OnTypeFormattingProvider } from '../OnTypeFormattingProvider'; |
| 7 | + |
| 8 | +const options: DocumentOnTypeFormattingParams['options'] = { |
| 9 | + insertSpaces: true, |
| 10 | + tabSize: 2, |
| 11 | +}; |
| 12 | + |
| 13 | +describe('Module: HtmlElementAutoclosingOnTypeFormattingProvider', () => { |
| 14 | + let documentManager: DocumentManager; |
| 15 | + let onTypeFormattingProvider: OnTypeFormattingProvider; |
| 16 | + let setCursorPositionSpy: ReturnType<typeof vi.fn>; |
| 17 | + const uri = 'file:///path/to/document.liquid'; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + setCursorPositionSpy = vi.fn(); |
| 21 | + documentManager = new DocumentManager(); |
| 22 | + onTypeFormattingProvider = new OnTypeFormattingProvider(documentManager, setCursorPositionSpy); |
| 23 | + vi.useFakeTimers(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + vi.useRealTimers(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return a TextEdit to insert a closing <tag> when you type > for an unclosed tag', async () => { |
| 31 | + const source = '<div id="main">'; |
| 32 | + documentManager.open(uri, source, 1); |
| 33 | + const document = documentManager.get(uri)?.textDocument!; |
| 34 | + |
| 35 | + const params: DocumentOnTypeFormattingParams = { |
| 36 | + textDocument: { uri }, |
| 37 | + position: document.positionAt(source.indexOf('>') + 1), |
| 38 | + ch: '>', |
| 39 | + options, |
| 40 | + }; |
| 41 | + |
| 42 | + assert(document); |
| 43 | + |
| 44 | + const result = await onTypeFormattingProvider.onTypeFormatting(params); |
| 45 | + assert(result); |
| 46 | + expect(TextDocument.applyEdits(document, result)).to.equal('<div id="main"></div>'); |
| 47 | + |
| 48 | + vi.advanceTimersByTime(10); |
| 49 | + expect(setCursorPositionSpy).toHaveBeenCalledWith( |
| 50 | + document, |
| 51 | + document.positionAt(source.indexOf('>') + 1), |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return a TextEdit to insert a closing <tag> when you type > for an unclosed tag even if the unclosed tag is higher up', async () => { |
| 56 | + const CURSOR = '█'; |
| 57 | + // in this scenario, the cursor is after div#inner, but the unclosed tag is div#main. |
| 58 | + // we still want to close it because that's probably what you want to do. |
| 59 | + const source = ` |
| 60 | + <div id="main"> |
| 61 | + <img> |
| 62 | + <div id="inner">█ |
| 63 | + </div> |
| 64 | + `; |
| 65 | + documentManager.open(uri, source.replace(CURSOR, ''), 1); |
| 66 | + const document = documentManager.get(uri)?.textDocument!; |
| 67 | + |
| 68 | + const indexOfCursor = source.indexOf(CURSOR); |
| 69 | + const params: DocumentOnTypeFormattingParams = { |
| 70 | + textDocument: { uri }, |
| 71 | + position: document.positionAt(indexOfCursor), |
| 72 | + ch: '>', |
| 73 | + options, |
| 74 | + }; |
| 75 | + |
| 76 | + assert(document); |
| 77 | + |
| 78 | + const result = await onTypeFormattingProvider.onTypeFormatting(params); |
| 79 | + assert(result); |
| 80 | + expect(TextDocument.applyEdits(document, result)).to.equal(` |
| 81 | + <div id="main"> |
| 82 | + <img> |
| 83 | + <div id="inner"></div> |
| 84 | + </div> |
| 85 | + `); |
| 86 | + |
| 87 | + vi.advanceTimersByTime(10); |
| 88 | + expect(setCursorPositionSpy).toHaveBeenCalledWith(document, document.positionAt(indexOfCursor)); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return a TextEdit to insert a closing <tag> when you type > for an unclosed tag even if the unclosed tag is higher up and theres a sibling closed tag', async () => { |
| 92 | + const CURSOR = '█'; |
| 93 | + // in this scenario, the cursor is after div#inner, but the unclosed tag is div#main. |
| 94 | + // we still want to close it because that's probably what you want to do. |
| 95 | + const source = ` |
| 96 | + <div id="main"> |
| 97 | + <div id="inner">█ |
| 98 | + <div></div> |
| 99 | + </div> |
| 100 | + `; |
| 101 | + documentManager.open(uri, source.replace(CURSOR, ''), 1); |
| 102 | + const document = documentManager.get(uri)?.textDocument!; |
| 103 | + |
| 104 | + const indexOfCursor = source.indexOf(CURSOR); |
| 105 | + const params: DocumentOnTypeFormattingParams = { |
| 106 | + textDocument: { uri }, |
| 107 | + position: document.positionAt(indexOfCursor), |
| 108 | + ch: '>', |
| 109 | + options, |
| 110 | + }; |
| 111 | + |
| 112 | + assert(document); |
| 113 | + |
| 114 | + const result = await onTypeFormattingProvider.onTypeFormatting(params); |
| 115 | + assert(result); |
| 116 | + expect(TextDocument.applyEdits(document, result)).to.equal(` |
| 117 | + <div id="main"> |
| 118 | + <div id="inner"></div> |
| 119 | + <div></div> |
| 120 | + </div> |
| 121 | + `); |
| 122 | + |
| 123 | + vi.advanceTimersByTime(10); |
| 124 | + expect(setCursorPositionSpy).toHaveBeenCalledWith(document, document.positionAt(indexOfCursor)); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should try to close dangling html open tags inside liquid branches', async () => { |
| 128 | + const CURSOR = '█'; |
| 129 | + const scenarios = [ |
| 130 | + '{% if cond %}<div>█{% endif %}', |
| 131 | + '{% if cond %}{% else %}<div>█{% endif %}', |
| 132 | + '{% unless cond %}<div>█{% endunless %}', |
| 133 | + '{% case thing %}{% when thing %}<div>█{% endif %}', |
| 134 | + ]; |
| 135 | + for (const source of scenarios) { |
| 136 | + documentManager.open(uri, source.replace(CURSOR, ''), 1); |
| 137 | + const document = documentManager.get(uri)?.textDocument!; |
| 138 | + const indexOfCursor = source.indexOf(CURSOR); |
| 139 | + const params: DocumentOnTypeFormattingParams = { |
| 140 | + textDocument: { uri }, |
| 141 | + position: document.positionAt(indexOfCursor), |
| 142 | + ch: '>', |
| 143 | + options, |
| 144 | + }; |
| 145 | + assert(document); |
| 146 | + const result = await onTypeFormattingProvider.onTypeFormatting(params); |
| 147 | + assert(result); |
| 148 | + expect(TextDocument.applyEdits(document, result)).to.equal(source.replace(CURSOR, '</div>')); |
| 149 | + |
| 150 | + vi.advanceTimersByTime(10); |
| 151 | + expect(setCursorPositionSpy).toHaveBeenCalledWith( |
| 152 | + document, |
| 153 | + document.positionAt(indexOfCursor), |
| 154 | + ); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + it('should not try to close the tag when pressing > inside a Liquid tag if condition (what HTML would do)', async () => { |
| 159 | + const CURSOR = '█'; |
| 160 | + const scenarios = ['<div {% if cond >█', '<div {% if cond >█ %}>', '<div {% if cond >█ 2 %}>']; |
| 161 | + for (const source of scenarios) { |
| 162 | + documentManager.open(uri, source.replace(CURSOR, ''), 1); |
| 163 | + const document = documentManager.get(uri)?.textDocument!; |
| 164 | + |
| 165 | + const indexOfCursor = source.indexOf(CURSOR); |
| 166 | + const params: DocumentOnTypeFormattingParams = { |
| 167 | + textDocument: { uri }, |
| 168 | + position: document.positionAt(indexOfCursor), |
| 169 | + ch: '>', |
| 170 | + options, |
| 171 | + }; |
| 172 | + |
| 173 | + assert(document); |
| 174 | + |
| 175 | + const result = await onTypeFormattingProvider.onTypeFormatting(params); |
| 176 | + assert(!result); |
| 177 | + } |
| 178 | + }); |
| 179 | +}); |
0 commit comments