`Matcher.highlight` highlights the wrong characters with markup or `İ`, and can raise `IndexError`
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- Medio día
- Aptitud para principiantes
- 55/100
Línea de trabajo
Start in src/textual/fuzzy.py, in Matcher.highlight, where the candidate is lowercased and offsets are used to index the original text, and where the markup is parsed but the query is matched against the raw string. Run the two reproductions from the issue to see the wrong span and the IndexError. Done when offsets map back to the displayed text for both the İ case and the markup case, and the palette shows both commands for 'ir'.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Hi! I hit this while writing generated tests against Textual's widgets.
Matcher.highlight() styles offsets that don't index the text it displays, in two ways.
1. Characters that lowercase to two code points. "İ".lower() is "i̇" (i plus U+0307), so in a case-insensitive match the offsets index the lowered candidate, which is one longer than the original. The highlight lands one character late, and on the last character it raises IndexError:
from textual.fuzzy import Matcher
print(Matcher("z").highlight("İzmir").spans)
print(Matcher("r").highlight("İzmir"))
Output (the same on 8.2.7 and on main at 5e5b7ef):
[Span(2, 3, style=Style(reverse=True))]
Traceback (most recent call last):
File "repro.py", line 4, in <module>
print(Matcher("r").highlight("İzmir"))
File ".../textual/fuzzy.py", line 217, in highlight
if not candidate[offset].isspace():
IndexError: string index out of range
The first line highlights m rather than z.
In the command palette the IndexError is raised inside the provider's search task, so it isn't reported; the provider's remaining commands just disappear. With these two commands, typing ir shows neither:
import asyncio
from textual.app import App, SystemCommand
from textual.command import CommandList
class PaletteApp(App):
def get_system_commands(self, screen):
yield SystemCommand("Weather in İzmir", "Turkey", self.bell)
yield SystemCommand("Weather in Izmir", "Turkey, ASCII spelling", self.bell)
async def main() -> None:
app = PaletteApp()
async with app.run_test() as pilot:
await pilot.press("ctrl+p")
await pilot.pause()
await pilot.press("i", "r")
await pilot.pause(0.5)
command_list = app.screen.query_one(CommandList)
print("commands shown for 'ir':", [str(option.prompt).splitlines()[0] for option in command_list.options])
asyncio.run(main())
commands shown for 'ir': []
With the fix below it prints ['Weather in İzmir', 'Weather in Izmir'].
2. Candidates containing markup. highlight() parses the candidate as markup, but matches the query against the raw string, so the offsets include the tags:
from textual.fuzzy import Matcher
content = Matcher("old").highlight("[b]Bold[/b] text")
print(repr(content.plain))
for span in content.spans:
print(span, repr(content.plain[span.start:span.end]))
Output (the same on 8.2.7 and on main):
'Bold text'
Span(0, 4, style='b') 'Bold'
Span(4, 5, style=Style(reverse=True)) ' '
Span(5, 6, style=Style(reverse=True)) 't'
Span(6, 7, style=Style(reverse=True)) 'e'
I'd expect o, l and d of "Bold" to be highlighted.
Textual 8.2.7 and main (5e5b7ef), Python 3.13, macOS.
- Lenguaje dominante
- Python
- Estrellas
- 37.7k
- Forks
- 1.5k
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Textualize/textual
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Textualize/textual#6790 · 2 comentarios ·
-
Markdown table of contents: the node added for a skipped heading level shows the wrong numeralAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 80/100
Textualize/textual#6789 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Textualize/textual#6787 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Textualize/textual#6782 · 1 comentario ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
Textualize/textual#6778 · 1 comentario ·
Todos los issues de Textualize/textual
Issues similares
-
Internal ErrorAbiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
frappe/frappe_docker#2071 ·
Los mantenedores suelen responder en 1 día
-
bug python
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
traceloop/openllmetry#4717 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
The-Pocket/PocketFlow#161 ·
-
[Bug]: Webapp workspace fails to render in Safari — "Can't find variable: requestIdleCallback"Abiertocomp/desktop P2 type/bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
NousResearch/hermes-agent#157702 ·
Los mantenedores suelen responder en 1 día
-
scibench_physics / scibench_chemistry: a correct answer scores 0 when the unit has a power of tenPosiblemente ocupada @BahaaMattar la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
EvolvingLMMs-Lab/lmms-eval#1699 ·
Los mantenedores suelen responder en 1 día