From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: haj@posteo.de (Harald =?UTF-8?Q?J=C3=B6rg?=) Newsgroups: gmane.emacs.bugs Subject: bug#23461: perl-mode: Displaying HERE-docs as strings instead of comments [PATCH] Date: Tue, 05 Jan 2021 00:43:55 +0100 Message-ID: <87czyk1d50.fsf@hajtower> References: <87sg7xxo0s.fsf@hajtower> <87zh24611c.fsf@hajtower> <87r1ng5pj8.fsf@hajtower> <87r1nfkyse.fsf@hajtower> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33105"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: Stefan Monnier To: 23461@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jan 05 00:48:15 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kwZaA-0008V1-96 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 05 Jan 2021 00:48:14 +0100 Original-Received: from localhost ([::1]:38814 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kwZa9-0004NR-8C for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 04 Jan 2021 18:48:13 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40792) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kwZX4-0002nk-Hn for bug-gnu-emacs@gnu.org; Mon, 04 Jan 2021 18:45:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:49043) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kwZX4-0006tJ-8Y for bug-gnu-emacs@gnu.org; Mon, 04 Jan 2021 18:45:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kwZX4-0001VD-6E for bug-gnu-emacs@gnu.org; Mon, 04 Jan 2021 18:45:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: haj@posteo.de (Harald =?UTF-8?Q?J=C3=B6rg?=) Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 04 Jan 2021 23:45:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 23461 X-GNU-PR-Package: emacs Original-Received: via spool by 23461-submit@debbugs.gnu.org id=B23461.16098038465698 (code B ref 23461); Mon, 04 Jan 2021 23:45:02 +0000 Original-Received: (at 23461) by debbugs.gnu.org; 4 Jan 2021 23:44:06 +0000 Original-Received: from localhost ([127.0.0.1]:60589 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kwZWA-0001Tq-2h for submit@debbugs.gnu.org; Mon, 04 Jan 2021 18:44:06 -0500 Original-Received: from mout02.posteo.de ([185.67.36.66]:58461) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kwZW7-0001TC-75 for 23461@debbugs.gnu.org; Mon, 04 Jan 2021 18:44:04 -0500 Original-Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 7499B2400FB for <23461@debbugs.gnu.org>; Tue, 5 Jan 2021 00:43:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1609803836; bh=ps90+8o1JhiTrcA8K70CX4qWFh7IqnAJyKVsUrnU0vk=; h=From:To:Cc:Subject:Date:From; b=SBBdiwtOyO/zoGS1jb53aioz69/decMZiKBC4vutO2oSC7p7s7LTN7nWF8cC65fjJ uM5AgFKRk3P4EN1FGex70SJO8y+HbENxMVbzHM5ghWNGTeHmIzyIt1b9pvBlf5Ufe0 JKOTMrDR346Mxfj6Bp1NX6yBEvjhf5NO6a9AOIsCk2rgSVKWURFfpCZ26macutpWCn UBFBqSKJdaSc9x4HU8FVcJEJHqkIiKXQtrdXBrfeuac1yyXvy3niIvoQOrd+67Fu0y WEvPVASDvU/W0JP5Alz/XnWaKFJGer+TJSUs+B8zyskaR7llu96fyVikmDR6C6thfN oKMOzL0WaompQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4D8sf34Vryz9rxG; Tue, 5 Jan 2021 00:43:55 +0100 (CET) In-Reply-To: (Stefan Monnier's message of "Thu, 24 Dec 2020 12:32:14 -0500") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:197344 Archived-At: --=-=-= Content-Type: text/plain Stefan Monnier writes: >> I have some doubts here regarding the effort and the side effects.... > > No doubt. :) In the meantime I found that just ignoring the edge case of a lone "#" at the end of a line starting HERE-docs breaks indentation after the HERE-doc, so I no longer consider this as a valid option. I just failed to get all cases covered when the HERE-doc starter is moved to the next line. So here's another attempt to show HERE-docs with their own face, treat "normal" line-end comments correctly, and work around lines which end with the comment starter. It is a kludge, but maybe for such a rare edge case a kludge is acceptable. The trick: The lone "#" is now syntaxified as "whitespace" and font-lock-faced as comment. This is ugly, but also well-contained in the offending line, so should not have unwanted effects at a distance. The tests are designed for, and pass, for both Perl mode and CPerl mode. -- Cheers, haj --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-perl-mode-Display-here-docs-as-strings-instead-of-co.patch Content-Description: perl-mode: Display HERE-docs with their own face >From 9a19e5991b33db7b8a71b653078f2a3c0a7afd08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20J=C3=B6rg?= Date: Tue, 5 Jan 2021 00:09:02 +0100 Subject: [PATCH] ; perl-mode: Display here-docs as strings instead of comments * lisp/progmodes/perl-mode.el (perl-syntax-propertize-function): Handle HERE doc starter lines ending in a comment. (perl-heredoc): New face for HERE docs, inheriting from font-lock-string-face. (perl-font-lock-syntactic-face-function): Apply the new face to HERE docs (Bug#23461). * test/lisp/progmodes/cperl-mode-tests.el (cperl-test--run-bug-10483): Skip for Perl mode. The test explicitly calls a function of CPerl mode. * test/lisp/progmodes/cperl-mode-resources/here-docs.pl: New tests for HERE docs, common to Perl mode and CPerl mode. --- lisp/progmodes/perl-mode.el | 29 +++- .../cperl-mode-resources/here-docs.pl | 143 ++++++++++++++++++ test/lisp/progmodes/cperl-mode-tests.el | 31 ++++ 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 test/lisp/progmodes/cperl-mode-resources/here-docs.pl diff --git a/lisp/progmodes/perl-mode.el b/lisp/progmodes/perl-mode.el index ec20b01a0f..2a2a4978c6 100644 --- a/lisp/progmodes/perl-mode.el +++ b/lisp/progmodes/perl-mode.el @@ -324,13 +324,33 @@ perl-syntax-propertize-function ;; disambiguate with the left-bitshift operator. "\\|" perl--syntax-exp-intro-regexp "<<\\(?2:\\sw+\\)\\)" ".*\\(\n\\)") - (4 (let* ((st (get-text-property (match-beginning 4) 'syntax-table)) + (4 (let* ((eol (match-beginning 4)) + (st (get-text-property eol 'syntax-table)) (name (match-string 2)) (indented (match-beginning 1))) (goto-char (match-end 2)) (if (save-excursion (nth 8 (syntax-ppss (match-beginning 0)))) + ;; '<<' occurred in a string, or in a comment. ;; Leave the property of the newline unchanged. st + ;; Beware of `foo <<'BAR' #baz` because + ;; the newline needs to start the here-doc + ;; and can't be used to close the comment. + (let ((eol-state (save-excursion (syntax-ppss eol)))) + (when (nth 4 eol-state) + (if (/= (1- eol) (nth 8 eol-state)) + ;; make the last char of the comment closing it + (put-text-property (1- eol) eol + 'syntax-table (string-to-syntax ">")) + ;; In `foo <<'BAR' #` the # is the last character + ;; before eol and can't both open and close the + ;; comment. Workaround: disguise the "#" as + ;; whitespace and fontify it as a comment. + (put-text-property (1- eol) eol + 'syntax-table (string-to-syntax "-")) + (put-text-property (1- eol) eol + 'font-lock-face + 'font-lock-comment-face)))) (cons (car (string-to-syntax "< c")) ;; Remember the names of heredocs found on this line. (cons (cons (pcase (aref name 0) @@ -483,8 +503,15 @@ perl-syntax-propertize-special-constructs ;; as twoarg). (perl-syntax-propertize-special-constructs limit))))))))) +(defface perl-heredoc + '((t (:inherit font-lock-string-face))) + "The face for here-documents. Inherits from font-lock-string-face.") + (defun perl-font-lock-syntactic-face-function (state) (cond + ((and (eq 2 (nth 7 state)) ; c-style comment + (cdr-safe (get-text-property (nth 8 state) 'syntax-table))) ; HERE doc + 'perl-heredoc) ((and (nth 3 state) (eq ?e (cdr-safe (get-text-property (nth 8 state) 'syntax-table))) ;; This is a second-arg of s{..}{...} form; let's check if this second diff --git a/test/lisp/progmodes/cperl-mode-resources/here-docs.pl b/test/lisp/progmodes/cperl-mode-resources/here-docs.pl new file mode 100644 index 0000000000..8af4625fff --- /dev/null +++ b/test/lisp/progmodes/cperl-mode-resources/here-docs.pl @@ -0,0 +1,143 @@ +use 5.020; + +=head1 NAME + +here-docs.pl - resource file for cperl-test-here-docs + +=head1 DESCRIPTION + +This file holds a couple of HERE documents, with a variety of normal +and edge cases. For a formatted view of this description, run: + + (cperl-perldoc "here-docs.pl") + +For each of the HERE documents, the following checks will done: + +=over 4 + +=item * + +All occurrences of the string "look-here" are fontified correcty. +Note that we deliberately test the face, not the syntax property: +Users won't care for the syntax property, but they see the face. +Different implementations with different syntax properties have been +seen in the past. + +=item * + +Indentation of the line(s) containing "look-here" is 0, i.e. there are no +leading spaces. + +=item * + +Indentation of the following perl statement containing "indent" should +be 0 if the statement contains "noindent", and according to the mode's +continued-statement-offset otherwise. + +=back + +=cut + +# Prologue to make the test file valid without warnings + +my $text; +my $any; +my $indentation; +my $anywhere = 'back again'; +my $noindent; + +=head1 The Tests + +=head2 Test Case 1 + +We have two HERE documents in one line with different quoting styles. + +=cut + +## test case + +$text = <<"HERE" . <<'THERE' . $any; +#look-here and +HERE +$tlook-here and +THERE + +$noindent = "This should be left-justified"; + +=head2 Test case 2 + +A HERE document followed by a continuation line + +=cut + +## test case + +$text = <