Skip to content

Commit

Permalink
Merge branch 'word-count'
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Jan 31, 2021
2 parents 890064a + 9c6bd6a commit 8adc89b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/nl/hannahsten/texifyidea/action/analysis/WordCountAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ open class WordCountAction : AnAction(
val fileSet = baseFile.referencedFileSet()
.filter { it.name.endsWith(".tex", ignoreCase = true) }
val allNormalText = fileSet.flatMap { it.childrenOfType(LatexNormalText::class) }
val parameterText = fileSet.flatMap { it.childrenOfType(LatexParameterText::class) }
.filter {
val commandText = it.command?.text ?: return@filter false
return@filter commandText !in IGNORE_COMMANDS
}

val bibliographies = baseFile.childrenOfType(LatexEnvironment::class)
.filter {
Expand All @@ -127,17 +132,18 @@ open class WordCountAction : AnAction(
val bibliography = bibliographies.flatMap { it.childrenOfType(LatexNormalText::class) }

val (wordsNormal, charsNormal) = countWords(allNormalText)
val (wordsParameter, charsParameter) = countWords(parameterText)
val (wordsBib, charsBib) = countWords(bibliography)

return Pair(wordsNormal - wordsBib, charsNormal - charsBib)
return Pair(wordsNormal + wordsParameter - wordsBib, charsNormal + charsParameter - charsBib)
}

/**
* Counts all the words in the text elements.
*
* @return A pair of the total amount of words, and the amount of characters that make up the words.
*/
private fun countWords(latexNormalText: List<LatexNormalText>): Pair<Int, Int> {
private fun countWords(latexNormalText: List<PsiElement>): Pair<Int, Int> {
// separate all latex words.
val latexWords: MutableSet<PsiElement> = HashSet()
var characters = 0
Expand Down
2 changes: 1 addition & 1 deletion src/nl/hannahsten/texifyidea/psi/LatexCommandsImplUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,4 @@ fun keyValContentToString(list: List<LatexKeyvalContent>): String =
}

fun keyValContentToString(element: LatexKeyvalValue): String =
keyValContentToString(element.keyvalContentList)
keyValContentToString(element.keyvalContentList)
7 changes: 6 additions & 1 deletion src/nl/hannahsten/texifyidea/psi/LatexParameterTextUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,9 @@ fun setName(element: LatexParameterText, name: String): PsiElement {

fun getName(element: LatexParameterText): String {
return element.text ?: ""
}
}

val LatexParameterText.command: PsiElement?
get() {
return this.firstParentOfType(LatexCommands::class)?.firstChild
}

0 comments on commit 8adc89b

Please sign in to comment.