Skip to content

Commit

Permalink
feat(textdocument): add length to LinesTextDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Apr 4, 2022
1 parent 394e172 commit 5d5cedd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/modules/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ describe('LinesTextDocument', () => {
expect(res).toBe('use std::io::{Result, Error};')
})

it('should get length', async () => {
let doc = createTextDocument(['foo'])
expect(doc.length).toBe(4)
expect(doc.getText().length).toBe(4)
expect(doc.length).toBe(4)
})

it('should work when eol enabled', async () => {
let doc = createTextDocument(['foo', 'bar'])
expect(doc.lineCount).toBe(3)
Expand Down
10 changes: 10 additions & 0 deletions src/model/textdocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ export class LinesTextDocument implements TextDocument {
return this._content
}

public get length(): number {
if (!this._content) {
let n = this.lines.reduce((p, c) => {
return p + c.length + 1
}, 0)
return this.eol ? n : n - 1
}
return this._content.length
}

public get end(): Position {
let line = this.lineCount - 1
if (this.eol) return Position.create(line, 0)
Expand Down
3 changes: 1 addition & 2 deletions src/sources/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ export default class KeywordsBuffer implements SyncItem {
let { textDocument } = this.doc
let { version, lineCount } = textDocument
if (this.version === version) return
let text = textDocument.getText()
if (events.insertMode
&& this.lineCount == lineCount
&& text.length > MAX_LENGTH) return
&& textDocument.length > MAX_LENGTH) return
this.cancel()
let tokenSource = this.tokenSource = new CancellationTokenSource()
void this.doc.matchWords(tokenSource.token).then(res => {
Expand Down
8 changes: 8 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,14 @@ declare module 'coc.nvim' {
}

export interface LinesTextDocument extends TextDocument {
/**
* Total length of TextDocument.
*/
readonly length: number
/**
* End position of TextDocument.
*/
readonly end: Position
/**
* Returns a text line denoted by the line number. Note
* that the returned object is *not* live and changes to the
Expand Down

0 comments on commit 5d5cedd

Please sign in to comment.