mirror of
https://github.com/latex-lsp/texlab.git
synced 2025-12-23 09:19:21 +00:00
Test renaming
This commit is contained in:
parent
7d319c7dc9
commit
4671d1ae5d
3 changed files with 101 additions and 6 deletions
|
|
@ -3,13 +3,14 @@ package texlab
|
|||
import org.eclipse.lsp4j.Position
|
||||
import texlab.completion.CompletionRequest
|
||||
import texlab.folding.FoldingRequest
|
||||
import texlab.rename.RenameRequest
|
||||
import java.io.File
|
||||
|
||||
class WorkspaceBuilder {
|
||||
val workspace = Workspace()
|
||||
|
||||
fun document(name: String, text: String): WorkspaceBuilder {
|
||||
val file = File(name)
|
||||
fun document(path: String, text: String): WorkspaceBuilder {
|
||||
val file = File(path)
|
||||
val language = getLanguageByExtension(file.extension)!!
|
||||
val document = Document.create(file.toURI(), language)
|
||||
document.text = text
|
||||
|
|
@ -18,14 +19,21 @@ class WorkspaceBuilder {
|
|||
return this
|
||||
}
|
||||
|
||||
fun completion(name: String, line: Int, character: Int): CompletionRequest {
|
||||
val uri = File(name).toURI()
|
||||
fun completion(path: String, line: Int, character: Int): CompletionRequest {
|
||||
val uri = File(path).toURI()
|
||||
val position = Position(line, character)
|
||||
return CompletionRequest(uri, workspace.relatedDocuments(uri), position)
|
||||
}
|
||||
|
||||
fun folding(name: String): FoldingRequest {
|
||||
val uri = File(name).toURI()
|
||||
fun folding(path: String): FoldingRequest {
|
||||
val uri = File(path).toURI()
|
||||
return FoldingRequest(workspace.documents.first { it.uri == uri })
|
||||
}
|
||||
|
||||
fun rename(path: String, line: Int, character: Int, newName: String): RenameRequest {
|
||||
val uri = File(path).toURI()
|
||||
val relatedDocuments = workspace.relatedDocuments(uri)
|
||||
val position = Position(line, character)
|
||||
return RenameRequest(uri, relatedDocuments, position, newName)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
43
src/test/kotlin/texlab/rename/LatexCommandRenamerTests.kt
Normal file
43
src/test/kotlin/texlab/rename/LatexCommandRenamerTests.kt
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package texlab.rename
|
||||
|
||||
import org.eclipse.lsp4j.Position
|
||||
import org.eclipse.lsp4j.Range
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import texlab.WorkspaceBuilder
|
||||
import java.io.File
|
||||
|
||||
class LatexCommandRenamerTests {
|
||||
@Test
|
||||
fun `it should rename commands in related documents`() {
|
||||
val edit = WorkspaceBuilder()
|
||||
.document("foo.tex", "\\include{bar.tex}\n\\baz")
|
||||
.document("bar.tex", "\\baz")
|
||||
.rename("foo.tex", 1, 2, "qux")
|
||||
.let { LatexCommandRenamer.rename(it) }!!
|
||||
|
||||
assertEquals(2, edit.changes.size)
|
||||
val document1 = File("foo.tex").toURI().toString()
|
||||
val document2 = File("bar.tex").toURI().toString()
|
||||
val change1 = edit.changes.getValue(document1)
|
||||
val change2 = edit.changes.getValue(document2)
|
||||
|
||||
assertEquals(1, change1.size)
|
||||
assertEquals(Range(Position(1, 0), Position(1, 4)), change1[0].range)
|
||||
assertEquals("\\qux", change1[0].newText)
|
||||
|
||||
assertEquals(1, change2.size)
|
||||
assertEquals(Range(Position(0, 0), Position(0, 4)), change2[0].range)
|
||||
assertEquals("\\qux", change2[0].newText)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `it should not process BibTeX documents`() {
|
||||
WorkspaceBuilder()
|
||||
.document("foo.bib", "\\foo \\bar")
|
||||
.rename("foo.bib", 0, 1, "baz")
|
||||
.let { LatexEnvironmentRenamer.rename(it) }
|
||||
.also { Assertions.assertNull(it) }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package texlab.rename
|
||||
|
||||
import org.eclipse.lsp4j.Position
|
||||
import org.eclipse.lsp4j.Range
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.Test
|
||||
import texlab.WorkspaceBuilder
|
||||
|
||||
class LatexEnvironmentRenamerTests {
|
||||
@Test
|
||||
fun `it should rename unmatched environments`() {
|
||||
val edit = WorkspaceBuilder()
|
||||
.document("foo.tex", "\\begin{foo}\n\\end{bar}")
|
||||
.rename("foo.tex", 0, 8, "baz")
|
||||
.let { LatexEnvironmentRenamer.rename(it) }!!
|
||||
|
||||
assertEquals(1, edit.changes.keys.size)
|
||||
val changes = edit.changes.getValue(edit.changes.keys.first())
|
||||
assertEquals(2, changes.size)
|
||||
assertEquals(Range(Position(0, 7), Position(0, 10)), changes[0].range)
|
||||
assertEquals("baz", changes[0].newText)
|
||||
assertEquals(Range(Position(1, 5), Position(1, 8)), changes[1].range)
|
||||
assertEquals("baz", changes[1].newText)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `it should not rename unrelated environments`() {
|
||||
WorkspaceBuilder()
|
||||
.document("foo.tex", "\\begin{foo}\n\\end{bar}")
|
||||
.rename("foo.tex", 0, 5, "baz")
|
||||
.let { LatexEnvironmentRenamer.rename(it) }
|
||||
.also { assertNull(it) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `it should not process BibTeX documents`() {
|
||||
WorkspaceBuilder()
|
||||
.document("foo.bib", "\\begin{foo}\n\\end{bar}")
|
||||
.rename("foo.bib", 0, 8, "baz")
|
||||
.let { LatexEnvironmentRenamer.rename(it) }
|
||||
.also { assertNull(it) }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue