Skip to content

Commit

Permalink
[iOS, Android]: Revert autocomplete duplication symbols logic (#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkash authored Jul 29, 2024
1 parent 55fcf36 commit a940512
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CodeAnalyzer(private val autocompleteContainer: AutocompleteContainer) {

private val quotes = hashMapOf(
"\"" to "\"",
"'" to "'",
"`" to "`"
)

Expand Down Expand Up @@ -84,9 +85,10 @@ class CodeAnalyzer(private val autocompleteContainer: AutocompleteContainer) {
}

in quotes -> {
val next = getNextSymbolAsString(start + 1, text)
val prev = getPrevSymbolAsString(start, text)
// don't want auto quote if there is a statement next
if (prev != inserted) {
if ((next == null || Character.isWhitespace(next[0])) && prev != inserted) {
insertTextAfterCursor(start, count, codeEditor, inserted)
} else {
onClosingSymbolInserted(start, count, codeEditor, inserted, text)
Expand Down
1 change: 1 addition & 0 deletions config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ID>ComposableParamOrder:LinearProgressIndicator.kt$LinearProgressIndicator</ID>
<ID>ComposableParamOrder:ServerSwitcher.kt$EndpointSwitcher</ID>
<ID>CyclomaticComplexMethod:AuthCredentialsReducer.kt$AuthCredentialsReducer$override fun reduce(state: State, message: Message): Pair&lt;State, Set&lt;Action&gt;&gt;</ID>
<ID>CyclomaticComplexMethod:CodeAnalyzer.kt$CodeAnalyzer$fun onTextInserted(start: Int, count: Int, codeEditor: CodeEditor)</ID>
<ID>CyclomaticComplexMethod:DebugReducer.kt$DebugReducer$override fun reduce(state: State, message: Message): Pair&lt;State, Set&lt;Action&gt;&gt;</ID>
<ID>CyclomaticComplexMethod:DefaultStepQuizFragment.kt$DefaultStepQuizFragment$override fun onAction(action: StepQuizFeature.Action.ViewAction)</ID>
<ID>CyclomaticComplexMethod:HomeReducer.kt$HomeReducer$override fun reduce(state: State, message: Message): HomeReducerResult</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ final class CodePlaygroundManager {
typealias AnalyzeResult = (text: String, position: Int, autocomplete: CodeCompletion?)
typealias Changes = (isInsertion: Bool, changes: String)

private static let closersDict = ["{": "}", "[": "]", "(": ")", "\"": "\""]
private static let closersDict = ["{": "}", "[": "]", "(": ")", "\"": "\"", "'": "'"]

private static let allowedCharactersSet = Set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_")

Expand Down Expand Up @@ -218,11 +218,37 @@ final class CodePlaygroundManager {

var text = currentText

text.insert(
closer[closer.startIndex],
at: currentText.index(currentText.startIndex, offsetBy: cursorPosition)
)
return (text: text, position: cursorPosition, autocomplete: nil)
// Check if there is text after the bracket, not a \n or whitespace
let cursorIndex = text.index(text.startIndex, offsetBy: cursorPosition)

if cursorIndex != text.endIndex {
let textAfter = String(text[cursorIndex...])
if let indexOfLineEndAfter = textAfter.indexOf("\n") {
let line = String(textAfter[..<textAfter.index(textAfter.startIndex, offsetBy: indexOfLineEndAfter)])

var onlySpaces = true
for character in line where character != " " {
onlySpaces = false
break
}

if onlySpaces {
text.insert(
closer[closer.startIndex],
at: currentText.index(currentText.startIndex, offsetBy: cursorPosition)
)
return (text: text, position: cursorPosition, autocomplete: nil)
}
}
} else {
text.insert(
closer[closer.startIndex],
at: currentText.index(currentText.startIndex, offsetBy: cursorPosition)
)
return (text: text, position: cursorPosition, autocomplete: nil)
}

return nil
}

private func getAutocompleteSuggestions(
Expand Down

0 comments on commit a940512

Please sign in to comment.