Skip to content

Commit

Permalink
fix(source-language): fix bad range
Browse files Browse the repository at this point in the history
Closes #3608
  • Loading branch information
chemzqm committed Jan 31, 2022
1 parent 19d68d3 commit c1a97aa
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion autoload/coc/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ endfunction

" used by vim
function! coc#util#get_buf_lines(bufnr, changedtick)
if !bufloaded(a:bufnr)
if !bufloaded(a:bufnr)
return v:null
endif
let changedtick = getbufvar(a:bufnr, 'changedtick')
Expand Down
21 changes: 17 additions & 4 deletions src/__tests__/modules/completion.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Neovim } from '@chemzqm/neovim'
import { Disposable } from 'vscode-jsonrpc'
import { CompletionItem, InsertTextFormat, Position, Range, TextEdit, CompletionList } from 'vscode-languageserver-types'
import { CompletionItem, CompletionList, InsertTextFormat, Position, Range, TextEdit } from 'vscode-languageserver-types'
import completion from '../../completion'
import languages from '../../languages'
import { CompletionItemProvider } from '../../provider'
Expand Down Expand Up @@ -645,9 +645,22 @@ describe('completion done', () => {
await nvim.input('<C-n>')
await helper.wait(30)
await nvim.call('coc#_select')
await helper.wait(100)
let line = await nvim.line
expect(line).toBe('football football')
await helper.waitFor('getline', ['.'], 'football football')
})

it('should fix bad range', async () => {
let provider: CompletionItemProvider = {
provideCompletionItems: async (): Promise<CompletionItem[]> => [{
label: 'foo',
filterText: 'foo',
textEdit: { range: Range.create(0, 0, 0, 0), newText: 'foo' },
}]
}
disposables.push(languages.registerCompletionItemProvider('edits', 'edit', null, provider))
await nvim.input('if')
await helper.waitPopup()
await helper.selectCompleteItem(0)
await helper.waitFor('getline', ['.'], 'foo')
})
})

Expand Down
11 changes: 4 additions & 7 deletions src/__tests__/modules/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ describe('workspace textDocument content provider', () => {
expect(lines).toEqual(['sample text'])
})

it('should react onChagne event of document content provider', async () => {
it('should react on change event of document content provider', async () => {
let text = 'foo'
let emitter = new Emitter<URI>()
let event = emitter.event
Expand All @@ -599,15 +599,12 @@ describe('workspace textDocument content provider', () => {
provideTextDocumentContent: (_uri, _token): string => text
}
workspace.registerTextDocumentContentProvider('jdk', provider)
await helper.wait(80)
workspace.autocmds.setupDynamicAutocmd(true)
await nvim.command('edit jdk://1')
await helper.wait(100)
await helper.wait(50)
text = 'bar'
emitter.fire(URI.parse('jdk://1'))
await helper.wait(200)
let buf = await nvim.buffer
let lines = await buf.lines
expect(lines).toEqual(['bar'])
await helper.waitFor('getline', ['.'], 'bar')
})
})

Expand Down
5 changes: 4 additions & 1 deletion src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ export default class Document {
if (this.env.isVim) {
if (currentLine) {
let change = await this.nvim.call('coc#util#get_changeinfo', []) as ChangeInfo
if (change.changedtick < this._changedtick) return
if (change.changedtick < this._changedtick) {
this._forceSync()
return
}
let { lnum, line, changedtick } = change
let curr = this.getline(lnum - 1)
this._changedtick = changedtick
Expand Down
1 change: 1 addition & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import events from './events'
import extensions from './extensions'
import Handler from './handler'
import listManager from './list/manager'
import fetch from './model/fetch'
import services from './services'
import snippetManager from './snippets/manager'
import sources from './sources'
Expand Down
2 changes: 1 addition & 1 deletion src/snippets/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class SnippetSession {
}
let position = range.start
const formatOptions = await workspace.getFormatOptions(this.document.uri)
await document.patchChange(true)
await document.patchChange()
const currentLine = document.getline(position.line)
const currentIndent = currentLine.match(/^\s*/)[0]
let inserted = ''
Expand Down
11 changes: 7 additions & 4 deletions src/sources/source-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,16 @@ export default class LanguageSource implements ISource {
let { nvim } = workspace
let { textEdit } = item
if (!textEdit) return false
let { line, bufnr, linenr } = option
let { line, bufnr, linenr, colnr } = option
let doc = workspace.getDocument(bufnr)
if (!doc) return false
let newText = textEdit.newText
let range = InsertReplaceEdit.is(textEdit) ? textEdit.replace : textEdit.range
let characterIndex = byteSlice(line, 0, colnr - 1).length
// attampt to fix range from textEdit, range should include trigger position
if (range.end.character < characterIndex) {
range.end.character = characterIndex
}
let isSnippet = item.insertTextFormat === InsertTextFormat.Snippet
// replace inserted word
let start = line.slice(0, range.start.character)
Expand Down Expand Up @@ -374,9 +379,7 @@ export function getWord(item: CompletionItem, opt: CompleteOption, invalidInsert
} else if (insertText) {
newText = insertText
}
if (insertTextFormat == InsertTextFormat.Snippet
&& newText
&& newText.includes('$')) {
if (insertTextFormat == InsertTextFormat.Snippet && newText && newText.includes('$')) {
let parser = new SnippetParser()
let text = parser.text(newText)
word = text ? getValidWord(text, invalidInsertCharacters) : label
Expand Down

0 comments on commit c1a97aa

Please sign in to comment.