Fix specs for testing highlighted lines

This commit is contained in:
Garen Torikian
2013-04-30 09:22:08 -07:00
committed by Garen Torikian & Nathan Sobo
parent cf239ccac0
commit 95e14486af
4 changed files with 50 additions and 34 deletions

View File

@@ -1765,7 +1765,7 @@ describe "Editor", ->
editor.edit(emptyEditSession)
expect(editor.gutter.lineNumbers.find('.line-number').length).toBe 1
describe "when the editor is mini", ->
fdescribe "when the editor is mini", ->
it "hides the gutter", ->
miniEditor = new Editor(mini: true)
miniEditor.attachToDom()

View File

@@ -1317,17 +1317,24 @@ class Editor extends View
buildLineElementsForScreenRows: (startRow, endRow) ->
div = document.createElement('div')
div.innerHTML = @buildLinesHtml(startRow, endRow)
div.innerHTML = @buildHtmlLines(startRow, endRow)
new Array(div.children...)
buildLinesHtml: (startRow, endRow) ->
buildHtmlLines: (startRow, endRow) ->
lines = @activeEditSession.linesForScreenRows(startRow, endRow)
htmlLines = []
screenRow = startRow
for line in @activeEditSession.linesForScreenRows(startRow, endRow)
htmlLines.push(@buildLineHtml(line, screenRow++))
htmlLines.push(@buildHtmlFromLine(line, screenRow++))
htmlLines.join('\n\n')
@buildEmptyLineHtml: (guideIndentation, showInvisibles, activeEditSession) ->
if guideIndentation > 0
indentationHtml = "<span class='indent-guide'>#{_.multiplyString(' ', @activeEditSession.getTabLength())}</span>"
return _.multiplyString(indentationHtml, indentation)
else if not showInvisibles
'&nbsp;'
buildEndOfLineInvisibles: (screenLine) ->
invisibles = []
for invisible in @getEndOfLineInvisibles(screenLine)
@@ -1337,12 +1344,19 @@ class Editor extends View
getEndOfLineInvisibles: (screenLine) ->
return [] unless @showInvisibles and @invisibles
return [] if @mini or screenLine.isSoftWrapped()
buildLineHtml: (screenLine, screenRow) ->
{ tokens, text, lineEnding, fold } = screenLine
if fold
attributes = { class: 'fold line', 'fold-id': fold.id }
else
attributes = { class: 'line' }
invisibles = []
invisibles.push(@invisibles.cr) if @invisibles.cr and screenLine.lineEnding is '\r\n'
invisibles.push(@invisibles.eol) if @invisibles.eol
invisibles
buildEmptyLineHtml: (screenRow) ->
if not @mini and @showIndentGuide
guideIndentation = 0
@@ -1353,13 +1367,10 @@ class Editor extends View
guideIndentation = Math.ceil(@activeEditSession.indentLevelForLine(bufferLine))
break
if indentation > 0
indentationHtml = "<span class='indent-guide'>#{_.multiplyString(' ', @activeEditSession.getTabLength())}</span>"
return _.multiplyString(indentationHtml, indentation)
invisibles = @invisibles if @showInvisibles
Editor.buildLineHtmlHelper({tokens, text, lineEnding, fold, isSoftWrapped, invisibles, attributes, guideIndentation, @showInvisibles, @activeEditSession, @mini})
return '&nbsp;' unless @showInvisibles
@buildLineHtmlHelper: ({tokens, text, lineEnding, fold, isSoftWrapped, attributes, guideIndentation, showInvisibles}) ->
@buildHtmlHelper: ({tokens, text, lineEnding, fold, isSoftWrapped, invisibles, attributes, guideIndentation, showInvisibles, activeEditSession, mini}) =>
scopeStack = []
line = []
@@ -1388,10 +1399,8 @@ class Editor extends View
attributePairs.push "#{attributeName}=\"#{value}\"" for attributeName, value of attributes
line.push("<div #{attributePairs.join(' ')}>")
invisibles = @invisibles if showInvisibles
if screenLine.text == ''
html = @buildEmptyLineHtml(screenRow)
if text == ''
html = Editor.buildEmptyLineHtml(guideIndentation, showInvisibles, activeEditSession)
line.push(html) if html
else
firstNonWhitespacePosition = text.search(/\S/)
@@ -1407,8 +1416,8 @@ class Editor extends View
position += token.value.length
popScope() while scopeStack.length > 0
if invisibles and not @mini and not screenLine.isSoftWrapped()
if invisibles.cr and screenLine.lineEnding is '\r\n'
if invisibles and not @mini and not isSoftWrapped
if invisibles.cr and lineEnding is '\r\n'
line.push("<span class='invisible-character'>#{invisibles.cr}</span>")
if invisibles.eol
line.push("<span class='invisible-character'>#{invisibles.eol}</span>")

View File

@@ -21,7 +21,7 @@ class MarkdownPreviewView extends ScrollView
initialize: (@buffer) ->
super
@fetchRenderedMarkdown()
@renderMarkdown()
@on 'core:move-up', => @scrollUp()
@on 'core:move-down', => @scrollDown()
@@ -71,22 +71,29 @@ class MarkdownPreviewView extends ScrollView
for codeBlock in preList.toArray()
codeBlock = $(codeBlock.firstChild)
if className = codeBlock.attr('class')
fenceName = className.replace(/^lang-/, '')
if extension = fenceNameToExtension[fenceName]
text = codeBlock.text()
syntax.selectGrammar("foo.#{extension}", text)
if grammar = syntax.selectGrammar("foo.#{extension}", text)
continue if grammar is syntax.nullGrammar
tokens = grammar.tokenizeLines(text)
grouping = ""
for token in tokens
grouping += Editor.buildLineHtml(token, text)
codeBlock.replaceWith(grouping)
# go to next block unless this one has a class
continue unless className = codeBlock.attr('class')
fenceName = className.replace(/^lang-/, '')
# go to next block unless the class name is matches `lang`
continue unless extension = fenceNameToExtension[fenceName]
text = codeBlock.text()
syntax.selectGrammar("foo.#{extension}", text)
# go to next block if this grammar is not mapped
continue unless grammar = syntax.selectGrammar("foo.#{extension}", text)
continue if grammar is syntax.nullGrammar
tokens = grammar.tokenizeLines(text)
grouping = ""
for token in tokens
grouping += Editor.buildHtmlLine(token, text)
codeBlock.replaceWith(grouping)
html
fetchRenderedMarkdown: ->
renderMarkdown: ->
@setLoading()
roaster(@buffer.getText(), {}, (err, html) =>
if err

View File

@@ -16,12 +16,12 @@ describe "MarkdownPreviewView", ->
describe "on construction", ->
it "shows a loading spinner and fetches the rendered markdown", ->
it "shows a loading spinner and renders the markdown", ->
preview.setLoading()
expect(preview.find('.markdown-spinner')).toExist()
expect(preview.buffer.getText()).toBe buffer.getText()
preview.fetchRenderedMarkdown()
preview.renderMarkdown()
expect(preview.find(".emoji")).toExist()
it "shows an error message on error", ->
@@ -33,7 +33,7 @@ describe "MarkdownPreviewView", ->
newPreview = deserialize(preview.serialize())
expect(newPreview.buffer).toBe buffer
fdescribe "code block tokenization", ->
describe "code block tokenization", ->
describe "when the code block's fence name has a matching grammar", ->
it "tokenizes the code block with the grammar", ->
expect(preview.find("pre span.entity.name.function.ruby")).toExist()