vscode-languageclient#State TypeScript Examples

The following examples show how to use vscode-languageclient#State. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: attrNameCase.ts    From volar with MIT License 4 votes vote down vote up
export async function activate(context: vscode.ExtensionContext, languageClient: BaseLanguageClient) {

	const attrCases = shared.createPathMap<'kebabCase' | 'camelCase'>();
	const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
	statusBar.command = 'volar.action.attrNameCase';

	onChangeDocument(vscode.window.activeTextEditor?.document);
	const d_1 = vscode.window.onDidChangeActiveTextEditor(e => {
		onChangeDocument(e?.document);
	});
	const d_2 = vscode.workspace.onDidCloseTextDocument((doc) => {
		attrCases.uriDelete(doc.uri.toString());
	});
	const d_3 = vscode.commands.registerCommand('volar.action.attrNameCase', async () => {

		const crtDoc = vscode.window.activeTextEditor?.document;
		if (!crtDoc) return;

		const attrCase = attrCases.uriGet(crtDoc.uri.toString());
		const options: Record<string, vscode.QuickPickItem> = {};

		options[4] = { label: (attrCase === 'kebabCase' ? '• ' : '') + 'Prop Using kebab-case' };
		options[5] = { label: (attrCase === 'camelCase' ? '• ' : '') + 'Prop Using camelCase' };
		options[6] = { label: 'Detect Prop name from Content' };

		const select = await userPick(options);
		if (select === undefined)
			return; // cancel

		if (select === '4') {
			attrCases.uriSet(crtDoc.uri.toString(), 'kebabCase');
			updateStatusBarText('kebabCase');
		}
		if (select === '5') {
			attrCases.uriSet(crtDoc.uri.toString(), 'camelCase');
			updateStatusBarText('camelCase');
		}
		if (select === '6') {
			const detects = await languageClient.sendRequest(shared.DetectDocumentNameCasesRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(crtDoc));
			if (detects) {
				attrCases.uriSet(crtDoc.uri.toString(), getValidAttrCase(detects.attr));
				updateStatusBarText(getValidAttrCase(detects.attr));
			}
		}
	});

	languageClient.onDidChangeState(e => {
		if (e.newState === State.Stopped) {
			d_1.dispose();
			d_2.dispose();
			d_3.dispose();
			statusBar.dispose();
		}
	});

	return (uri: string) => {
		let attrCase = attrCases.uriGet(uri);
		if (uri.toLowerCase() === vscode.window.activeTextEditor?.document.uri.toString().toLowerCase()) {
			updateStatusBarText(attrCase);
		}
		return attrCase ?? 'kebabCase';
	};

	async function onChangeDocument(newDoc: vscode.TextDocument | undefined) {
		if (newDoc?.languageId === 'vue') {
			let attrCase = attrCases.uriGet(newDoc.uri.toString());
			if (!attrCase) {
				const attrMode = vscode.workspace.getConfiguration('volar').get<'auto-kebab' | 'auto-camel' | 'kebab' | 'camel'>('completion.preferredAttrNameCase');
				if (attrMode === 'kebab') {
					attrCase = 'kebabCase';
				}
				else if (attrMode === 'camel') {
					attrCase = 'camelCase';
				}
				else {
					const templateCases = await languageClient.sendRequest(shared.DetectDocumentNameCasesRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(newDoc));
					if (templateCases) {
						attrCase = getValidAttrCase(templateCases.attr);
						if (templateCases.attr === 'both') {
							if (attrMode === 'auto-kebab') {
								attrCase = 'kebabCase';
							}
							else if (attrMode === 'auto-camel') {
								attrCase = 'camelCase';
							}
						}
					}
				}
			}
			if (attrCase) {
				attrCases.uriSet(newDoc.uri.toString(), attrCase ?? 'unsure');
			}
			updateStatusBarText(attrCase);
			statusBar.show();
		}
		else {
			statusBar.hide();
		}
	}
	function getValidAttrCase(attrCase: 'both' | 'kebabCase' | 'camelCase' | 'unsure' | undefined): 'kebabCase' | 'camelCase' {
		if (attrCase === 'both' || attrCase === 'unsure') {
			const attrMode = vscode.workspace.getConfiguration('volar').get<'auto-kebab' | 'auto-camel' | 'kebab' | 'camel'>('completion.preferredAttrNameCase');
			if (attrMode === 'auto-kebab') {
				return 'kebabCase';
			}
			else if (attrMode === 'auto-camel') {
				return 'camelCase';
			}
			return 'kebabCase';
		}
		return attrCase ?? 'kebabCase';
	}
	function updateStatusBarText(
		attrCase: 'kebabCase' | 'camelCase' | undefined,
	) {
		let text = `Attr: `;
		if (attrCase === 'kebabCase' || attrCase === undefined) {
			text += `kebab-case`;
		}
		else if (attrCase === 'camelCase') {
			text += `camelCase`;
		}
		statusBar.text = text;
	}
}
Example #2
Source File: tagNameCase.ts    From volar with MIT License 4 votes vote down vote up
export async function activate(context: vscode.ExtensionContext, languageClient: BaseLanguageClient) {

	const tagCases = shared.createPathMap<'both' | 'kebabCase' | 'pascalCase' | 'unsure'>();
	const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
	statusBar.command = 'volar.action.tagNameCase';

	onChangeDocument(vscode.window.activeTextEditor?.document);

	const d_1 = vscode.window.onDidChangeActiveTextEditor(e => {
		onChangeDocument(e?.document);
	});
	const d_2 = vscode.workspace.onDidCloseTextDocument((doc) => {
		tagCases.uriDelete(doc.uri.toString());
	});
	const d_3 = vscode.commands.registerCommand('volar.action.tagNameCase', async () => {

		const crtDoc = vscode.window.activeTextEditor?.document;
		if (!crtDoc) return;

		const tagCase = tagCases.uriGet(crtDoc.uri.toString());
		const options: Record<string, vscode.QuickPickItem> = {};

		options[0] = { label: (tagCase === 'both' ? '• ' : '') + 'Component Using kebab-case and PascalCase (Both)' };
		options[1] = { label: (tagCase === 'kebabCase' ? '• ' : '') + 'Component Using kebab-case' };
		options[2] = { label: (tagCase === 'pascalCase' ? '• ' : '') + 'Component Using PascalCase' };
		options[3] = { label: 'Detect Component name from Content' };
		options[7] = { label: 'Convert Component name to kebab-case' };
		options[8] = { label: 'Convert Component name to PascalCase' };

		const select = await userPick(options);
		if (select === undefined)
			return; // cancel

		if (select === '0') {
			tagCases.uriSet(crtDoc.uri.toString(), 'both');
			updateStatusBarText('both');
		}
		if (select === '1') {
			tagCases.uriSet(crtDoc.uri.toString(), 'kebabCase');
			updateStatusBarText('kebabCase');
		}
		if (select === '2') {
			tagCases.uriSet(crtDoc.uri.toString(), 'pascalCase');
			updateStatusBarText('pascalCase');
		}
		if (select === '3') {
			const detects = await languageClient.sendRequest(shared.DetectDocumentNameCasesRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(crtDoc));
			if (detects) {
				tagCases.uriSet(crtDoc.uri.toString(), detects.tag);
				updateStatusBarText(detects.tag);
			}
		}
		if (select === '7') {
			vscode.commands.executeCommand('volar.action.tagNameCase.convertToKebabCase');
		}
		if (select === '8') {
			vscode.commands.executeCommand('volar.action.tagNameCase.convertToPascalCase');
		}
	});
	const d_4 = vscode.commands.registerCommand('volar.action.tagNameCase.convertToKebabCase', async () => {
		if (vscode.window.activeTextEditor) {
			await vscode.commands.executeCommand('volar.server.convertTagNameCasing', vscode.window.activeTextEditor.document.uri.toString(), 'kebab');
			tagCases.uriSet(vscode.window.activeTextEditor.document.uri.toString(), 'kebabCase');
			updateStatusBarText('kebabCase');
		}
	});
	const d_5 = vscode.commands.registerCommand('volar.action.tagNameCase.convertToPascalCase', async () => {
		if (vscode.window.activeTextEditor) {
			await vscode.commands.executeCommand('volar.server.convertTagNameCasing', vscode.window.activeTextEditor.document.uri.toString(), 'pascal');
			tagCases.uriSet(vscode.window.activeTextEditor.document.uri.toString(), 'pascalCase');
			updateStatusBarText('pascalCase');
		}
	});

	languageClient.onDidChangeState(e => {
		if (e.newState === State.Stopped) {
			d_1.dispose();
			d_2.dispose();
			d_3.dispose();
			d_4.dispose();
			d_5.dispose();
			statusBar.dispose();
		}
	});

	return (uri: string) => {
		let tagCase = tagCases.uriGet(uri);
		if (uri.toLowerCase() === vscode.window.activeTextEditor?.document.uri.toString().toLowerCase()) {
			updateStatusBarText(tagCase);
		}
		return !tagCase || tagCase === 'unsure' ? 'both' : tagCase;
	};

	async function onChangeDocument(newDoc: vscode.TextDocument | undefined) {
		if (newDoc?.languageId === 'vue') {
			let tagCase = tagCases.uriGet(newDoc.uri.toString());
			if (!tagCase) {
				const tagMode = vscode.workspace.getConfiguration('volar').get<'auto' | 'both' | 'kebab' | 'pascal'>('completion.preferredTagNameCase');
				if (tagMode === 'both') {
					tagCase = 'both';
				}
				else if (tagMode === 'kebab') {
					tagCase = 'kebabCase';
				}
				else if (tagMode === 'pascal') {
					tagCase = 'pascalCase';
				}
				else {
					const templateCases = await languageClient.sendRequest(shared.DetectDocumentNameCasesRequest.type, languageClient.code2ProtocolConverter.asTextDocumentIdentifier(newDoc));
					tagCase = templateCases?.tag;
				}
			}
			if (tagCase) {
				tagCases.uriSet(newDoc.uri.toString(), tagCase);
			}
			updateStatusBarText(tagCase);
			statusBar.show();
		}
		else {
			statusBar.hide();
		}
	}
	function updateStatusBarText(tagCase: 'both' | 'kebabCase' | 'pascalCase' | 'unsure' | undefined) {
		let text = `Tag: `;
		if (tagCase === 'unsure' || tagCase === undefined) {
			text += `UNSURE`;
		}
		else if (tagCase === 'both') {
			text += `BOTH`;
		}
		else if (tagCase === 'kebabCase') {
			text += `kebab-case`;
		}
		else if (tagCase === 'pascalCase') {
			text += `PascalCase`;
		}
		statusBar.text = text;
	}
}