Skip to content

InsertQuoteAction overwrites trailing character after token instead of closing double quote in Python #539

@paul-griffith

Description

@paul-griffith

Description
In (at least) Python syntax highlighting, the character after the quote you just closed can be removed/overwritten.
Kapture 2024-03-22 at 08 30 38

<speculation>
This appears to be caused by the token maker. In the exact same scenario for Java, there will be a token "something with a type of ERROR_STRING_DOUBLE. But for Python, you get a token "something with a type of LITERAL_STRING_DOUBLE_QUOTE. This leads to a different branch in InsertQuoteAction, specifically this one:

			if (tokenType == quoteType.validTokenType) {
				if (offs == t.getEndOffset() - 1) {
					textArea.moveCaretPosition(offs + 1); // Force a replacement to ensure undo is contiguous
					textArea.replaceSelection(stringifiedQuoteTypeCh);
					textArea.setCaretPosition(offs + 1);
				}

This seems to be the cause of the issue, with the root being the token type spit out by the token maker.
</speculation>

Steps to Reproduce

  1. Type a string such as str("something) into an RSyntaxTextArea with Python syntax highlighting.
  2. Set the caret position between g and ).
  3. Enter a double quote.

Here's a test case that reproduces in RSyntaxTextAreaEditorKitInsertQuoteActionTest:

	@Test
	void testActionPerformedImpl_python_close_quote_overtypes() {
		String origContent = "call(\"function)";

		RSyntaxTextArea textArea = createTextArea(SyntaxConstants.SYNTAX_STYLE_PYTHON, origContent);
		textArea.setCaretPosition(origContent.indexOf("function") + 8);

		RecordableTextAction a = new RSyntaxTextAreaEditorKit.InsertQuoteAction("test",
			RSyntaxTextAreaEditorKit.InsertQuoteAction.QuoteType.DOUBLE_QUOTE);
		ActionEvent e = createActionEvent(textArea, "\"");
		a.actionPerformedImpl(e, textArea);

		Assertions.assertEquals("call(\"function\")", textArea.getText());
	}

Expected behavior
Double quotes should be closed 'correctly' without overtyping.

I'll also note, for the sake of it, that single quotes work just fine.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions