Move largeFileMode logic to TokenizedBuffer

This commit is contained in:
Max Brunsfeld
2017-11-06 16:30:40 -08:00
parent 52f70997b6
commit fe6b385c97
5 changed files with 33 additions and 22 deletions

View File

@@ -85,22 +85,6 @@ describe('TextEditor', () => {
})
})
describe('when the editor is constructed with the largeFileMode option set to true', () => {
it("loads the editor but doesn't tokenize", async () => {
editor = await atom.workspace.openTextFile('sample.js', {largeFileMode: true})
buffer = editor.getBuffer()
expect(editor.lineTextForScreenRow(0)).toBe(buffer.lineForRow(0))
expect(editor.tokensForScreenRow(0).length).toBe(1)
expect(editor.tokensForScreenRow(1).length).toBe(2) // soft tab
expect(editor.lineTextForScreenRow(12)).toBe(buffer.lineForRow(12))
expect(editor.getCursorScreenPosition()).toEqual([0, 0])
editor.insertText('hey"')
expect(editor.tokensForScreenRow(0).length).toBe(1)
expect(editor.tokensForScreenRow(1).length).toBe(2)
})
})
describe('.copy()', () => {
it('returns a different editor with the same initial state', () => {
expect(editor.getAutoHeight()).toBeFalsy()

View File

@@ -33,6 +33,34 @@ describe('TokenizedBuffer', () => {
}
}
describe('when the editor is constructed with the largeFileMode option set to true', () => {
it("loads the editor but doesn't tokenize", async () => {
const line = 'a b c d\n'
buffer = new TextBuffer(line.repeat(256 * 1024))
expect(buffer.getText().length).toBe(2 * 1024 * 1024)
tokenizedBuffer = new TokenizedBuffer({
buffer,
grammar: atom.grammars.grammarForScopeName('source.js'),
tabLength: 2
})
buffer.setLanguageMode(tokenizedBuffer)
expect(tokenizedBuffer.isRowCommented(0)).toBeFalsy()
// It treats the entire line as one big token
let iterator = tokenizedBuffer.buildHighlightIterator()
iterator.seek({row: 0, column: 0})
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual({row: 0, column: 7})
buffer.insert([0, 0], 'hey"')
iterator = tokenizedBuffer.buildHighlightIterator()
iterator.seek({row: 0, column: 0})
iterator.moveToSuccessor()
expect(iterator.getPosition()).toEqual({row: 0, column: 11})
})
})
describe('serialization', () => {
describe('when the underlying buffer has a path', () => {
beforeEach(async () => {

View File

@@ -123,7 +123,6 @@ class TextEditor {
this.mini = (params.mini != null) ? params.mini : false
this.placeholderText = params.placeholderText
this.showLineNumbers = params.showLineNumbers
this.largeFileMode = params.largeFileMode
this.assert = params.assert || (condition => condition)
this.showInvisibles = (params.showInvisibles != null) ? params.showInvisibles : true
this.autoHeight = params.autoHeight
@@ -528,7 +527,6 @@ class TextEditor {
mini: this.mini,
editorWidthInChars: this.editorWidthInChars,
width: this.width,
largeFileMode: this.largeFileMode,
maxScreenLineLength: this.maxScreenLineLength,
registered: this.registered,
invisibles: this.invisibles,

View File

@@ -29,6 +29,9 @@ class TokenizedBuffer {
this.tabLength = params.tabLength
this.largeFileMode = params.largeFileMode
this.config = params.config
this.largeFileMode = params.largeFileMode != null
? params.largeFileMode
: this.buffer.buffer.getLength() >= 2 * 1024 * 1024
this.grammar = params.grammar || NullGrammar
this.rootScopeDescriptor = new ScopeDescriptor({scopes: [this.grammar.scopeName]})

View File

@@ -1211,9 +1211,7 @@ module.exports = class Workspace extends Model {
}
const fileSize = fs.getSizeSync(filePath)
const largeFileMode = fileSize >= (2 * 1048576) // 2MB
if (fileSize >= (this.config.get('core.warnOnLargeFileLimit') * 1048576)) { // 20MB by default
if (fileSize >= (this.config.get('core.warnOnLargeFileLimit') * 1048576)) {
const choice = this.applicationDelegate.confirm({
message: 'Atom will be unresponsive during the loading of very large files.',
detailedMessage: 'Do you still want to load this file?',
@@ -1228,7 +1226,7 @@ module.exports = class Workspace extends Model {
return this.project.bufferForPath(filePath, options)
.then(buffer => {
return this.textEditorRegistry.build(Object.assign({buffer, largeFileMode, autoHeight: false}, options))
return this.textEditorRegistry.build(Object.assign({buffer, autoHeight: false}, options))
})
}