Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let pos: Position = insertLeave ? { line: position.line + 1, character: 0 } : position
try {
let edits = await languages.provideDocumentOntTypeEdits(ch, doc.textDocument, pos)
// changed by other process
if (doc.changedtick != changedtick) return
if (insertLeave) {
edits = edits.filter(edit => {
return edit.range.start.line < position.line + 1
})
}
if (edits && edits.length) {
await doc.applyEdits(this.nvim, edits)
let newLine = doc.getline(position.line)
if (newLine.length > origLine.length) {
let character = position.character + (newLine.length - origLine.length)
await workspace.moveTo(Position.create(position.line, character))
}
}
} catch (e) {
if (!/timeout\s/.test(e.message)) {
console.error(`Error on formatOnType: ${e.message}`) // tslint:disable-line
}
}
}
if (!textEdit) return false
let { line, bufnr, linenr } = option
let doc = workspace.getDocument(bufnr)
if (!doc) return false
let { range, newText } = textEdit
let isSnippet = item.insertTextFormat === InsertTextFormat.Snippet
// replace inserted word
let start = line.substr(0, range.start.character)
let end = line.substr(range.end.character)
if (isSnippet) {
await doc.applyEdits(nvim, [{
range: Range.create(linenr - 1, 0, linenr, 0),
newText: `${start}${end}\n`
}])
// can't select, since additionalTextEdits would break selection
let pos = Position.create(linenr - 1, range.start.character)
return await snippetManager.insertSnippet(newText, false, Range.create(pos, pos))
}
let newLines = `${start}${newText}${end}`.split('\n')
if (newLines.length == 1) {
await nvim.call('coc#util#setline', [linenr, newLines[0]])
await workspace.moveTo(Position.create(linenr - 1, (start + newText).length))
} else {
let buffer = nvim.createBuffer(bufnr)
await buffer.setLines(newLines, {
start: linenr - 1,
end: linenr,
strictIndexing: false
})
let line = linenr - 1 + newLines.length - 1
let character = newLines[newLines.length - 1].length - end.length
await workspace.moveTo({ line, character })
// replace inserted word
let start = line.substr(0, range.start.character)
let end = line.substr(range.end.character)
if (isSnippet) {
await doc.applyEdits(nvim, [{
range: Range.create(linenr - 1, 0, linenr, 0),
newText: `${start}${end}\n`
}])
// can't select, since additionalTextEdits would break selection
let pos = Position.create(linenr - 1, range.start.character)
return await snippetManager.insertSnippet(newText, false, Range.create(pos, pos))
}
let newLines = `${start}${newText}${end}`.split('\n')
if (newLines.length == 1) {
await nvim.call('coc#util#setline', [linenr, newLines[0]])
await workspace.moveTo(Position.create(linenr - 1, (start + newText).length))
} else {
let buffer = nvim.createBuffer(bufnr)
await buffer.setLines(newLines, {
start: linenr - 1,
end: linenr,
strictIndexing: false
})
let line = linenr - 1 + newLines.length - 1
let character = newLines[newLines.length - 1].length - end.length
await workspace.moveTo({ line, character })
}
return false
}
let curr = doc.getline(line)
let prevChar = pre[pre.length - 1]
if (prevChar && pairs.has(prevChar)) {
let nextChar = curr.trim()[0]
if (nextChar && pairs.get(prevChar) == nextChar) {
let edits: TextEdit[] = []
let opts = await workspace.getFormatOptions(doc.uri)
let space = opts.insertSpaces ? ' '.repeat(opts.tabSize) : '\t'
let preIndent = pre.match(/^\s*/)[0]
let currIndent = curr.match(/^\s*/)[0]
let newText = '\n' + preIndent + space
let pos: Position = Position.create(line - 1, pre.length)
// make sure indent of current line
if (preIndent != currIndent) {
let newText = doc.filetype == 'vim' ? ' \\ ' + preIndent : preIndent
edits.push({ range: Range.create(Position.create(line, 0), Position.create(line, currIndent.length)), newText })
} else if (doc.filetype == 'vim') {
edits.push({ range: Range.create(line, currIndent.length, line, currIndent.length), newText: ' \\ ' })
}
if (doc.filetype == 'vim') {
newText = newText + '\\ '
}
edits.push({ range: Range.create(pos, pos), newText })
await doc.applyEdits(nvim, edits)
await workspace.moveTo(Position.create(line, newText.length - 1))
}
}
}
}, null, this.disposables)
public async provideDocumentFormattingEdits(document: TextDocument, options: FormattingOptions): Promise {
if (!this.formatManager.hasProvider(document)) {
let hasRangeFormater = this.formatRangeManager.hasProvider(document)
if (!hasRangeFormater) {
logger.error('Format provider not found for current document', 'error')
return null
}
let end = document.positionAt(document.getText().length)
let range = Range.create(Position.create(0, 0), end)
return await this.provideDocumentRangeFormattingEdits(document, range, options)
}
return await this.formatManager.provideDocumentFormattingEdits(document, options, this.token)
}
public async getCursorPosition(): Promise {
let [line, character] = await this.nvim.call('coc#util#cursor')
return Position.create(line, character)
}
async lineRange(uri: PathLike | URI, lineNumber: number): Promise {
const line = await this.lineAt(uri, lineNumber);
return Range.create(
Position.create(lineNumber, line.search(/\S|$/)),
Position.create(lineNumber, line.replace(/\s+$/, '').length)
);
}
public get range(): Range {
let { position } = this
const content = this.tmSnippet.toString()
const doc = TextDocument.create('untitled:/1', 'snippet', 0, content)
const pos = doc.positionAt(content.length)
const end = pos.line == 0 ? position.character + pos.character : pos.character
return Range.create(position, Position.create(position.line + pos.line, end))
}
public getLocalifyBonus(sp: Position, ep: Position): Map {
let res: Map = new Map()
let { chars } = this
let startLine = Math.max(0, sp.line - 100)
let endLine = Math.min(this.lineCount, sp.line + 100)
let content = this.lines.slice(startLine, endLine).join('\n')
sp = Position.create(sp.line - startLine, sp.character)
ep = Position.create(ep.line - startLine, ep.character)
let doc = TextDocument.create(this.uri, this.filetype, 1, content)
let headCount = doc.offsetAt(sp)
let len = content.length
let tailCount = len - doc.offsetAt(ep)
let start = 0
let preKeyword = false
for (let i = 0; i < headCount; i++) {
let iskeyword = chars.isKeyword(content[i])
if (!preKeyword && iskeyword) {
start = i
} else if (preKeyword && (!iskeyword || i == headCount - 1)) {
if (i - start > 1) {
let str = content.slice(start, i)
res.set(str, i / headCount)
}