export function getFilePath(documentUri: string): string { const IS_WINDOWS = platform() === 'win32'; if (IS_WINDOWS) { // Windows have a leading slash like /C:/Users/pine return Uri.parse(documentUri).path.slice(1); } else { return Uri.parse(documentUri).path; } }
export function getUri(fullpath: string, id: number, buftype: string): string { if (!fullpath) return `untitled:${id}` if (path.isAbsolute(fullpath)) return Uri.file(fullpath).toString() if (isuri.isValid(fullpath)) return Uri.parse(fullpath).toString() if (buftype != '') return `${buftype}:${id}` return `unknown:${id}` }
function resolveWorkspaceRoot(activeDoc: TextDocument, workspaceFolders: WorkspaceFolder[]): string | undefined { for (let i = 0; i < workspaceFolders.length; i++) { if (startsWith(activeDoc.uri, workspaceFolders[i].uri)) { return path.resolve(URI.parse(workspaceFolders[i].uri).fsPath); } } }
export function ensureLeadingDot<T extends string>(href: T): T { if (!Uri.parse(href).scheme && !(href.startsWith('./') || href.startsWith('../'))) { return './' + href as T; } return href; }
it('should fire onWillSaveUntil', async () => { let doc = await helper.createDocument() let filepath = Uri.parse(doc.uri).fsPath let fn = jest.fn() let disposable = workspace.onWillSaveUntil(event => { let promise = new Promise<TextEdit[]>(resolve => { fn() let edit: TextEdit = { newText: 'foo', range: Range.create(0, 0, 0, 0) } resolve([edit]) }) event.waitUntil(promise) }, null, 'test') await helper.wait(100) await nvim.setLine('bar') await helper.wait(30) await events.fire('BufWritePre', [doc.bufnr]) await helper.wait(30) let content = doc.getDocumentContent() expect(content.startsWith('foobar')).toBe(true) disposable.dispose() expect(fn).toBeCalledTimes(1) if (fs.existsSync(filepath)) { fs.unlinkSync(filepath) } })
function getFormatter( document: TextDocument, env: IEnvironment, config: RubyConfiguration, range?: Range ): IFormatter { if (typeof config.format === 'string') { const formatterConfig: FormatterConfig = { env, executionRoot: URI.parse(config.workspaceFolderUri).fsPath, config: { command: config.format, useBundler: config.useBundler, }, }; if (range) { formatterConfig.range = range; } return new FORMATTER_MAP[config.format](document, formatterConfig); } else { return new NullFormatter(); } }
function providePathSuggestions(pathValue: string, position: Position, range: Range, document: TextDocument, workspaceFolders: WorkspaceFolder[]) { const fullValue = stripQuotes(pathValue); const isValueQuoted = startsWith(pathValue, `'`) || startsWith(pathValue, `"`); const valueBeforeCursor = isValueQuoted ? fullValue.slice(0, position.character - (range.start.character + 1)) : fullValue.slice(0, position.character - range.start.character); const workspaceRoot = resolveWorkspaceRoot(document, workspaceFolders); const currentDocFsPath = URI.parse(document.uri).fsPath; const paths = providePaths(valueBeforeCursor, currentDocFsPath, workspaceRoot) .filter(p => { // Exclude current doc's path return path.resolve(currentDocFsPath, '../', p) !== currentDocFsPath; }) .filter(p => { // Exclude paths that start with `.` return p[0] !== '.'; }); const fullValueRange = isValueQuoted ? shiftRange(range, 1, -1) : range; const replaceRange = pathToReplaceRange(valueBeforeCursor, fullValue, fullValueRange); const suggestions = paths.map(p => pathToSuggestion(p, replaceRange)); return suggestions; }
it('should not create file if document exists', async () => { let doc = await helper.createDocument() let filepath = URI.parse(doc.uri).fsPath await workspace.createFile(filepath, { ignoreIfExists: false }) let exists = fs.existsSync(filepath) expect(exists).toBe(false) })
function createLink( document: TextDocument, documentContext: DocumentContext, attributeValue: string, startOffset: number, endOffset: number, base: string ): DocumentLink | null { const documentUri = Uri.parse(document.uri); const tokenContent = stripQuotes(attributeValue); if (tokenContent.length === 0) { return null; } if (tokenContent.length < attributeValue.length) { startOffset++; endOffset--; } const workspaceUrl = getWorkspaceUrl(documentUri, tokenContent, documentContext, base); if (!workspaceUrl || !isValidURI(workspaceUrl)) { return null; } return { range: Range.create(document.positionAt(startOffset), document.positionAt(endOffset)), target: workspaceUrl }; }
export function ensureLeadingDot<T>(href: T): T { const hrefString = href as any as string; if (!Uri.parse(hrefString).scheme && !(hrefString.startsWith('./') || hrefString.startsWith('../'))) { return './' + href as any as T; } return href; }