Repository: microsoft/vscode (TypeScript)
Date: 2023-04-19 19:29:29+00:00
Task type: Bug Fix

Issue:
VS Code crash ("The window is not responding") after running "Organize Usings"
This was filed at https://github.com/Dart-Code/Dart-Code/issues/4486, but I believe the crash is a VS Code issue.

I can reproduce the issue by doing the following:

- Set up a Dart SDK and install the Dart extension
- Open an empty folder and create a `foo.dart` file with the following contents (**and no trailing newline**):
    ```dart
    export 'interior_material_selector.dart';
    export 'mileage_selector.dart';
    export 'owners_selector.dart';
    export 'price_selector.dart';
    export 'seat_count_selector.dart';
    export 'year_selector.dart';
    export 'winter_options_selector.dart';export 'camera_selector.dart';
    ```
- Run the "Organize Imports" command
- VS Code immediately hangs and the cursor stops flashing, eventually reporting the crash and asking to reopen/close

I ran the Dart language server from source and stripped out a lot of the capabilities to reduce the traffic. When removing the Semantic Tokens capability, the issue stops occurring, so it may be related to semantic tokens but I'm not certain.

Here is the LSP log (the version running the reduced set of Dart server capabilities):

```
==> {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":11548,"clientInfo":{"name":"Visual Studio Code","version":"1.77.0"},"locale":"en-gb","rootPath":"/Users/danny/Desktop/dart_samples/dartcode_4486","rootUri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486","capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional","normalizesLineEndings":true,"changeAnnotationSupport":{"groupsOnLabel":true}},"configuration":true,"didChangeWatchedFiles":{"dynamicRegistration":true,"relativePatternSupport":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]},"resolveSupport":{"properties":["location.range"]}},"codeLens":{"refreshSupport":true},"executeCommand":{"dynamicRegistration":true},"didChangeConfiguration":{"dynamicRegistration":true},"workspaceFolders":true,"semanticTokens":{"refreshSupport":true},"fileOperations":{"dynamicRegistration":true,"didCreate":true,"didRename":true,"didDelete":true,"willCreate":true,"willRename":true,"willDelete":true},"inlineValue":{"refreshSupport":true},"inlayHint":{"refreshSupport":true},"diagnostics":{"refreshSupport":true}},"textDocument":{"publishDiagnostics":{"relatedInformation":true,"versionSupport":false,"tagSupport":{"valueSet":[1,2]},"codeDescriptionSupport":true,"dataSupport":true},"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true,"tagSupport":{"valueSet":[1]},"insertReplaceSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"insertTextModeSupport":{"valueSet":[1,2]},"labelDetailsSupport":true},"insertTextMode":2,"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]},"completionList":{"itemDefaults":["commitCharacters","editRange","insertTextFormat","insertTextMode"]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true},"activeParameterSupport":true},"contextSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"hierarchicalDocumentSymbolSupport":true,"tagSupport":{"valueSet":[1]},"labelSupport":true},"codeAction":{"dynamicRegistration":true,"isPreferredSupport":true,"disabledSupport":true,"dataSupport":true,"resolveSupport":{"properties":["edit"]},"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}},"honorsChangeAnnotations":false},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true,"prepareSupportDefaultBehavior":1,"honorsChangeAnnotations":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true,"foldingRangeKind":{"valueSet":["comment","imports","region"]},"foldingRange":{"collapsedText":false}},"declaration":{"dynamicRegistration":true,"linkSupport":true},"selectionRange":{"dynamicRegistration":true},"callHierarchy":{"dynamicRegistration":true},"semanticTokens":{"dynamicRegistration":true,"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator","decorator"],"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],"formats":["relative"],"requests":{"range":true,"full":{"delta":true}},"multilineTokenSupport":false,"overlappingTokenSupport":false,"serverCancelSupport":true,"augmentsSyntaxTokens":true},"linkedEditingRange":{"dynamicRegistration":true},"typeHierarchy":{"dynamicRegistration":true},"inlineValue":{"dynamicRegistration":true},"inlayHint":{"dynamicRegistration":true,"resolveSupport":{"properties":["tooltip","textEdits","label.tooltip","label.location","label.command"]}},"diagnostic":{"dynamicRegistration":true,"relatedDocumentSupport":false}},"window":{"showMessage":{"messageActionItem":{"additionalPropertiesSupport":true}},"showDocument":{"support":true},"workDoneProgress":true},"general":{"staleRequestSupport":{"cancel":true,"retryOnContentModified":["textDocument/semanticTokens/full","textDocument/semanticTokens/range","textDocument/semanticTokens/full/delta"]},"regularExpressions":{"engine":"ECMAScript","version":"ES2020"},"markdown":{"parser":"marked","version":"1.1.0","allowedTags":["ul","li","p","code","blockquote","ol","h1","h2","h3","h4","h5","h6","hr","em","pre","table","thead","tbody","tr","th","td","div","del","a","strong","br","img","span"]},"positionEncodings":["utf-16"]},"notebookDocument":{"synchronization":{"dynamicRegistration":true,"executionSummarySupport":true}},"experimental":{"supportsWindowShowMessageRequest":true,"snippetTextEdit":true,"dartCodeAction":{"commandParameterSupport":{"supportedKinds":["saveUri"]}}}},"initializationOptions":{"allowOpenUri":true,"appHost":"desktop","closingLabels":true,"flutterOutline":false,"onlyAnalyzeProjectsWithOpenFiles":false,"outline":true,"suggestFromUnimportedLibraries":true},"trace":"off","workspaceFolders":[{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486","name":"dartcode_4486"}]},"clientRequestTime":1680702758109}
<== {"id":0,"jsonrpc":"2.0","result":{"capabilities":{"executeCommandProvider":{"commands":["edit.sortMembers","edit.organizeImports","edit.fixAll","edit.sendWorkspaceEdit","refactor.perform","move_top_level_to_file"],"workDoneProgress":true},"workspace":{"workspaceFolders":{"changeNotifications":true,"supported":true}},"workspaceSymbolProvider":true},"serverInfo":{"name":"Dart SDK LSP Analysis Server","version":"3.0.0-edge.c57cbd0c193d1f2cfdfcbdd730c9cc9103cea857"}}}
==> {"jsonrpc":"2.0","method":"initialized","params":{},"clientRequestTime":1680702767138}
<== {"id":1,"jsonrpc":"2.0","method":"client/registerCapability","params":{"registrations":[{"id":"0","method":"textDocument/didOpen","registerOptions":{"documentSelector":[{"language":"dart","scheme":"file"},{"language":"yaml","pattern":"**/pubspec.yaml","scheme":"file"},{"language":"yaml","pattern":"**/analysis_options.yaml","scheme":"file"},{"language":"yaml","pattern":"**/lib/fix_data.yaml","scheme":"file"}]}},{"id":"1","method":"textDocument/didClose","registerOptions":{"documentSelector":[{"language":"dart","scheme":"file"},{"language":"yaml","pattern":"**/pubspec.yaml","scheme":"file"},{"language":"yaml","pattern":"**/analysis_options.yaml","scheme":"file"},{"language":"yaml","pattern":"**/lib/fix_data.yaml","scheme":"file"}]}},{"id":"2","method":"textDocument/didChange","registerOptions":{"documentSelector":[{"language":"dart","scheme":"file"},{"language":"yaml","pattern":"**/pubspec.yaml","scheme":"file"},{"language":"yaml","pattern":"**/analysis_options.yaml","scheme":"file"},{"language":"yaml","pattern":"**/lib/fix_data.yaml","scheme":"file"}],"syncKind":2}},{"id":"3","method":"textDocument/codeAction","registerOptions":{"codeActionKinds":["source","source.organizeImports","source.fixAll","source.sortMembers","quickfix","refactor"],"documentSelector":[{"language":"dart","scheme":"file"}]}},{"id":"4","method":"textDocument/semanticTokens","registerOptions":{"documentSelector":[{"language":"dart","scheme":"file"}],"full":{"delta":false},"legend":{"tokenModifiers":["documentation","constructor","declaration","importPrefix","instance","static","escape","annotation","control","label","interpolation","void"],"tokenTypes":["annotation","keyword","class","comment","method","variable","parameter","enum","enumMember","type","source","property","namespace","boolean","number","string","function","typeParameter"]},"range":true}}]}}
==> {"jsonrpc":"2.0","id":1,"result":null,"clientRequestTime":1680702767197}
==> {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart","languageId":"dart","version":1,"text":"export 'interior_material_selector.dart';\nexport 'mileage_selector.dart';\nexport 'owners_selector.dart';\nexport 'price_selector.dart';\nexport 'seat_count_selector.dart';\nexport 'year_selector.dart';\nexport 'winter_options_selector.dart';export 'camera_selector.dart';"}},"clientRequestTime":1680702767197}
==> {"jsonrpc":"2.0","id":1,"method":"textDocument/codeAction","params":{"textDocument":{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart"},"range":{"start":{"line":6,"character":68},"end":{"line":6,"character":68}},"context":{"diagnostics":[],"triggerKind":2}},"clientRequestTime":1680702767198}
<== {"id":1,"jsonrpc":"2.0","result":[]}
==> {"jsonrpc":"2.0","id":2,"method":"textDocument/semanticTokens/range","params":{"textDocument":{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart"},"range":{"start":{"line":0,"character":0},"end":{"line":6,"character":68}}},"clientRequestTime":1680702767502}
==> {"jsonrpc":"2.0","id":3,"method":"textDocument/semanticTokens/full","params":{"textDocument":{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart"}},"clientRequestTime":1680702767828}
<== {"id":2,"jsonrpc":"2.0","result":{"data":[0,0,6,1,0,0,7,33,15,0,1,0,6,1,0,0,7,23,15,0,1,0,6,1,0,0,7,22,15,0,1,0,6,1,0,0,7,21,15,0,1,0,6,1,0,0,7,26,15,0,1,0,6,1,0,0,7,20,15,0,1,0,6,1,0,0,7,30,15,0,0,31,6,1,0,0,7,22,15,0]}}
<== {"id":3,"jsonrpc":"2.0","result":{"data":[0,0,6,1,0,0,7,33,15,0,1,0,6,1,0,0,7,23,15,0,1,0,6,1,0,0,7,22,15,0,1,0,6,1,0,0,7,21,15,0,1,0,6,1,0,0,7,26,15,0,1,0,6,1,0,0,7,20,15,0,1,0,6,1,0,0,7,30,15,0,0,31,6,1,0,0,7,22,15,0]}}
==> {"jsonrpc":"2.0","id":4,"method":"textDocument/codeAction","params":{"textDocument":{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart"},"range":{"start":{"line":6,"character":68},"end":{"line":6,"character":68}},"context":{"diagnostics":[],"only":["source.organizeImports"],"triggerKind":1}},"clientRequestTime":1680702777207}
<== {"id":4,"jsonrpc":"2.0","result":[{"command":{"arguments":[{"path":"/Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart"}],"command":"edit.organizeImports","title":"Organize Imports"},"kind":"source.organizeImports","title":"Organize Imports"}]}
==> {"jsonrpc":"2.0","id":5,"method":"workspace/executeCommand","params":{"command":"edit.organizeImports","arguments":[{"path":"/Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart"}]},"clientRequestTime":1680702777229}
<== {"id":2,"jsonrpc":"2.0","method":"workspace/applyEdit","params":{"edit":{"documentChanges":[{"edits":[{"newText":"export 'camera_selector.dart';\nexport 'interior_material_selector.dart';\nexport 'mileage_selector.dart';\nexport 'owners_selector.dart';\nexport 'price_selector.dart';\nexport 'seat_count_selector.dart';\nexport 'winter_options_selector.dart';\nexport 'year","range":{"end":{"character":52,"line":6},"start":{"character":0,"line":0}}}],"textDocument":{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart","version":1}}]},"label":"Organize Imports"}}
==> {"jsonrpc":"2.0","method":"textDocument/didChange","params":{"textDocument":{"uri":"file:///Users/danny/Desktop/dart_samples/dartcode_4486/bin/dartcode_4486.dart","version":2},"contentChanges":[{"range":{"start":{"line":6,"character":51},"end":{"line":6,"character":52}},"rangeLength":1,"text":""},{"range":{"start":{"line":6,"character":48},"end":{"line":6,"character":50}},"rangeLength":2,"text":""},{"range":{"start":{"line":6,"character":46},"end":{"line":6,"character":47}},"rangeLength":1,"text":"ye"},{"range":{"start":{"line":6,"character":38},"end":{"line":6,"character":38}},"rangeLength":0,"text":"\n"},{"range":{"start":{"line":5,"character":8},"end":{"line":6,"character":8}},"rangeLength":29,"text":""},{"range":{"start":{"line":0,"character":8},"end":{"line":0,"character":8}},"rangeLength":0,"text":"camera_selector.dart';\nexport '"}]},"clientRequestTime":1680702777265}
==> {"jsonrpc":"2.0","id":2,"result":{"applied":true},"clientRequestTime":1680702777271}
<== {"id":5,"jsonrpc":"2.0","result":null}
```

Here is the crash-dump folder: [crash_dump.zip](https://github.com/microsoft/vscode/files/11159574/crash_dump.zip)
Symbolicated: [symbolicated.zip](https://github.com/microsoft/vscode/files/11159827/symbolicated.zip), though https://github.com/microsoft/vscode-electron-prebuilt (mentioned at https://github.com/Microsoft/vscode/wiki/Native-Crash-Issues) is a 404 so these may be incomplete.

I've not had any luck reproducing this without the Dart extension/server by writing extension code that just applies the same edits (possibly because it requires semantic tokens) nor have I spotted anything unusual in the LSP traffic, but I'll have another go through these and post back if I figure anything out. Hopefully the crash log will help though.

VS Code version:

```
Version: 1.77.0
Commit: 7f329fe6c66b0f86ae1574c2911b681ad5a45d63
Date: 2023-03-29T09:57:11.797Z
Electron: 19.1.11
Chromium: 102.0.5005.196
Node.js: 16.14.2
V8: 10.2.154.26-electron.0
OS: Darwin x64 22.2.0
Sandboxed: Yes
```


Issue discussion:
I was able to reproduce this in an isolated test extension by just hard-coding semantic tokens and the edits:

https://github.com/DanTup/vscode-repro-sort-imports-crash

The complete code looks like this (though you'll need to contribute the "danny" language or change it.. I didn't want to reuse an existing language incase it had its own semantic tokens):

```ts
export async function activate(context: vscode.ExtensionContext) {

	vscode.languages.registerDocumentSemanticTokensProvider(
		{ language: "danny" },
		{
			provideDocumentSemanticTokens(document, token) {
				// Copied from the repro.. we only get asked once, for the original file so this is fine (and if it's not, it shouldn't crash)
				return new vscode.SemanticTokens(new Uint32Array([0, 0, 6, 1, 0, 0, 7, 33, 15, 0, 1, 0, 6, 1, 0, 0, 7, 23, 15, 0, 1, 0, 6, 1, 0, 0, 7, 22, 15, 0, 1, 0, 6, 1, 0, 0, 7, 21, 15, 0, 1, 0, 6, 1, 0, 0, 7, 26, 15, 0, 1, 0, 6, 1, 0, 0, 7, 20, 15, 0, 1, 0, 6, 1, 0, 0, 7, 30, 15, 0, 0, 31, 6, 1, 0, 0, 7, 22, 15, 0]), undefined);
			},
		},
		{
			"tokenModifiers": ["documentation", "constructor", "declaration", "importPrefix", "instance", "static", "escape", "annotation", "control", "label", "interpolation", "void"],
			"tokenTypes": ["annotation", "keyword", "class", "comment", "method", "variable", "parameter", "enum", "enumMember", "type", "source", "property", "namespace", "boolean", "number", "string", "function", "typeParameter"]
		},
	);

	await new Promise((resolve) => setTimeout(resolve, 1000));

	const content1 = "export 'interior_material_selector.dart';\nexport 'mileage_selector.dart';\nexport 'owners_selector.dart';\nexport 'price_selector.dart';\nexport 'seat_count_selector.dart';\nexport 'year_selector.dart';\nexport 'winter_options_selector.dart';export 'camera_selector.dart';";
	const content2 = "export 'camera_selector.dart';\nexport 'interior_material_selector.dart';\nexport 'mileage_selector.dart';\nexport 'owners_selector.dart';\nexport 'price_selector.dart';\nexport 'seat_count_selector.dart';\nexport 'winter_options_selector.dart';\nexport 'year";

	const doc = await vscode.workspace.openTextDocument({ language: 'danny', content: content1 });
	const editor = await vscode.window.showTextDocument(doc);
	await new Promise((resolve) => setTimeout(resolve, 3000));

	const edit = new vscode.WorkspaceEdit();
	edit.replace(doc.uri, new vscode.Range(new vscode.Position(0, 0), new vscode.Position(6, 52)), content2);
	await vscode.workspace.applyEdit(edit);
}
```
Doesn't seem to reproduce in the [monaco editor](https://hediet.github.io/monaco-editor/playground.html?source=v0.38.0-dev.20230418#XQAAAALRBwAAAAAAAABBqQkHQ5NjdMjwa-jY7SIQ9S7DNlzs5W-mwj0fe1ZCDRFc9ws9XQE0SJE1jc2VKxhaLFIw9vEWSxW3yscxCY2Qnc4m7i-ttPEXkmoxCsimZnMDyzxcqZuywuN-I5rCKKBqRq0aleNrdvm6v-IMHaVcORHk0BYWB2hNhHoi-fhp4K6KqN91yyAnDG5JcDm36dD95JALeinzOc5nCf1wMhavlLnLXqarmg8kX4f3YTKEp7zD4PippJRj2qNx6mAnfLb3OGdYnF58QS7i52S9cUrbkbUdJBIRPwB9ZiU9N_61IY5tED3iJeWdM9bCO_EHKZTIjWKFV7jSRahJITK_yBsNBbLoKEGOWUUQuTRqH_2JAwHuqyiYm8iFFkCkykk34IzYZSySKY_1pT5wxV-E6LAS-3nwCVxIr5gb1JGOPn4wq9T352emwHP6qt0DPAvUWgT_3TCQTXRMZwQ43pdYxaLOnehLZ3mpTgJj6DlKIOdAg2i736cawqk8YA2vLIgOb4bVJpUKpc3lg8Q5uEehgVaZ_hVEIM0cRYA6JQw3faY5U44yoDHCTCQGrzEKABT2WU0WsnDsbwL-O96K8Y_oJJRNogR_EIaKAAJIVq2OLVYkFSu2QC3RWS82y_eeKU1dXxtKKJX4nTj8oiK53RHEOfsblk7TMiUe6r_Lag_hwxg1lCm3VMDUybZ9LhSUae6UEkpPJHy1ZLCx9DAwToIXCb0zmkZXA_RLy0gGB47N1F0LEXm_zOcALs0jgEhTU9CIjoDJYhuu8bfbB2IB_PQeKTEXMIRWesg-SZ22EG2KRKi8qOtikwQz1H9atlzo1fX-KJSWMK0twabG9sFXP29bTgDecAM0TcrLJod0uhpbluCP_qQVZjNI7BrPsq8hXh3F6u7CXWwlqcN-Zx-GLrAh9L1yiWghkTpt8hMDCKkjxmEo_IoIVUvHKBkIoaVpZbOnVKVDzLToQ74CwcqrhTplFwMVD0IZykVEs6ezdDDk0EgCZV4l9bAULtjv17a11yqoA-IvAvju_egTh8WVxBQUG3EEAYYv-_Izl0PbJ01doAsVXlZYJeHySahs-g8qG2sqjzy9s8-ybM9aZR8Ye16q3eFs7Z90hEt4d498d0wto__SiKi2).

However, @alexdima, I can observe in VS Code that LineTokens is created with unsorted offsets:
```ts
constructor(tokens: Uint32Array, text: string, decoder: ILanguageIdCodec) {
for (let i = 2, len = tokens.length; i < len; i += 2) {
	if (tokens[i - 2] > tokens[i]) {
		console.log('Tokens are not sorted!');
	}
}
}
```

Solution patch:
diff --git a/src/vs/editor/common/tokens/sparseMultilineTokens.ts b/src/vs/editor/common/tokens/sparseMultilineTokens.ts
--- a/src/vs/editor/common/tokens/sparseMultilineTokens.ts
+++ b/src/vs/editor/common/tokens/sparseMultilineTokens.ts
@@ -431,18 +431,10 @@ class SparseMultilineTokensStorage {
 				// 3a, 3b, 3c
 				if (tokenDeltaLine === endDeltaLine && tokenEndCharacter > endCharacter) {
 					// 3c. The token starts inside the deletion range, and ends after the deletion range
-					// => the token moves left and shrinks
-					if (tokenDeltaLine === startDeltaLine) {
-						// the deletion started on the same line as the token
-						// => the token moves left and shrinks
-						tokenStartCharacter = startCharacter;
-						tokenEndCharacter = tokenStartCharacter + (tokenEndCharacter - endCharacter);
-					} else {
-						// the deletion started on a line above the token
-						// => the token moves to the beginning of the line
-						tokenStartCharacter = 0;
-						tokenEndCharacter = tokenStartCharacter + (tokenEndCharacter - endCharacter);
-					}
+					// => the token moves to continue right after the deletion
+					tokenDeltaLine = startDeltaLine;
+					tokenStartCharacter = startCharacter;
+					tokenEndCharacter = tokenStartCharacter + (tokenEndCharacter - endCharacter);
 				} else {
 					// 3a. The token is inside the deletion range
 					// 3b. The token starts inside the deletion range, and ends at the same position as the deletion range


Test patch:
diff --git a/src/vs/editor/test/common/model/tokensStore.test.ts b/src/vs/editor/test/common/model/tokensStore.test.ts
--- a/src/vs/editor/test/common/model/tokensStore.test.ts
+++ b/src/vs/editor/test/common/model/tokensStore.test.ts
@@ -99,8 +99,6 @@ suite('TokensStore', () => {
 		return result;
 	}
 
-	// function extractState
-
 	function testTokensAdjustment(rawInitialState: string[], edits: ISingleEditOperation[], rawFinalState: string[]) {
 		const initialState = parseTokensState(rawInitialState);
 		const model = createTextModel(initialState.text);
@@ -175,6 +173,38 @@ suite('TokensStore', () => {
 		);
 	});
 
+	test('issue #179268: a complex edit', () => {
+		testTokensAdjustment(
+			[
+				`|export| |'interior_material_selector.dart'|;`,
+				`|export| |'mileage_selector.dart'|;`,
+				`|export| |'owners_selector.dart'|;`,
+				`|export| |'price_selector.dart'|;`,
+				`|export| |'seat_count_selector.dart'|;`,
+				`|export| |'year_selector.dart'|;`,
+				`|export| |'winter_options_selector.dart'|;|export| |'camera_selector.dart'|;`
+			],
+			[
+				{ range: new Range(1, 9, 1, 9), text: `camera_selector.dart';\nexport '` },
+				{ range: new Range(6, 9, 7, 9), text: `` },
+				{ range: new Range(7, 39, 7, 39), text: `\n` },
+				{ range: new Range(7, 47, 7, 48), text: `ye` },
+				{ range: new Range(7, 49, 7, 51), text: `` },
+				{ range: new Range(7, 52, 7, 53), text: `` },
+			],
+			[
+				`|export| |'|camera_selector.dart';`,
+				`export 'interior_material_selector.dart';`,
+				`|export| |'mileage_selector.dart'|;`,
+				`|export| |'owners_selector.dart'|;`,
+				`|export| |'price_selector.dart'|;`,
+				`|export| |'seat_count_selector.dart'|;`,
+				`|export| |'||winter_options_selector.dart'|;`,
+				`|export| |'year_selector.dart'|;`
+			]
+		);
+	});
+
 	test('issue #91936: Semantic token color highlighting fails on line with selected text', () => {
 		const model = createTextModel('                    else if ($s = 08) then \'\\b\'');
 		model.tokenization.setSemanticTokens([