Implementing AI-Powered Code Assistant in Mobile Applications
Mobile Code Assist isn't just "chat with GPT about code." It's an editor with syntax highlighting, autocomplete, code explanation, and ability to execute requests in the context of open file. Target audience: mobile IDEs, educational platforms, code review tools on phone.
Code editor: basic requirements
Standard UITextView/EditText doesn't work for code. Need syntax highlighting, monospace font, horizontal scroll, correct Tab/indent handling.
iOS: Runestone (open-source, Tree-sitter grammars) or Sourceful. Android: CodeEditor from Rosemoe or embed WebView with CodeMirror/Monaco.
// iOS with Runestone
import Runestone
let textView = TextView()
textView.theme = OneDarkTheme()
textView.language = TreeSitterLanguage.swift // or python, js, etc.
textView.font = UIFont.monospacedSystemFont(ofSize: 14, weight: .regular)
textView.showLineNumbers = true
textView.isLineWrappingEnabled = false // horizontal scroll for code
Tree-sitter grammars available for 50+ languages. Highlighting incremental—on line change, only changed portion reparses, not whole file. Critical for large files.
AI part: contextual requests
Code Assist must understand context: open language, code around cursor, selected text. All goes in prompt.
struct CodeAssistRequest {
let userQuestion: String
let codeContext: CodeContext
}
struct CodeContext {
let language: String
let fullCode: String // entire file, if < 3000 tokens
let selectionStart: Int
let selectionEnd: Int
let cursorLine: Int
var selectedCode: String {
String(fullCode.utf16.prefix(selectionEnd).dropFirst(selectionStart)) ?? ""
}
var surroundingContext: String {
// 50 lines around cursor
let lines = fullCode.components(separatedBy: "\n")
let from = max(0, cursorLine - 25)
let to = min(lines.count, cursorLine + 25)
return lines[from..<to].joined(separator: "\n")
}
}
func buildMessages(for request: CodeAssistRequest) -> [ChatMessage] {
let systemPrompt = """
You are a \(request.codeContext.language) expert. Answer questions about the provided code.
When suggesting code changes, output only the changed code block.
Language: \(request.codeContext.language)
"""
let userMessage = """
Code context:
```\(request.codeContext.language)
\(request.codeContext.surroundingContext)
```
\(request.codeContext.selectedCode.isEmpty ? "" : "Selected code:\n```\n\(request.codeContext.selectedCode)\n```\n")
Question: \(request.userQuestion)
"""
return [
ChatMessage(role: "system", content: systemPrompt),
ChatMessage(role: "user", content: userMessage)
]
}
Parsing code blocks from response
LLM returns text with markdown code blocks. Parse and offer to apply.
// Android
fun parseCodeBlocks(response: String): List<CodeBlock> {
val regex = Regex("```(\\w+)?\\n([\\s\\S]*?)```")
return regex.findAll(response).map { match ->
CodeBlock(
language = match.groupValues[1].ifEmpty { "plaintext" },
code = match.groupValues[2].trimEnd()
)
}.toList()
}
data class CodeBlock(val language: String, val code: String)
UI: "Apply" button next to each code block. Apply via replaceSelection() or insert at specific file position.
Chat history and multi-turn requests
One code question rarely suffices. Need dialog history—but not all, else context overflows.
Strategy: store last 6 question/answer pairs + original system prompt with file context. On file change, update system prompt—recreate history.
Important nuance: code blocks in LLM responses consume many tokens. When storing history, replace large code blocks with [code block replaced, N lines]—preserves dialog context without inflating request.
On-device models for code
For products where code privacy matters: Ollama with codellama:7b or qwen2.5-coder:7b on user's server. Full on-device on mobile phone still unrealistic for code—minimum working model needs ~4 GB RAM.
iOS 18+ Apple Intelligence provides on-device LLM via Foundation Models, but limited context and no code specialization. Not alternative for code assist.
Timeline estimates
Editor with highlighting + basic Q&A chat—1 week. Full Code Assist with file context, code block parsing, apply changes, chat history—3–4 weeks.







