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: Wed, 23 Dec 2020 15:37:51 +0100 Message-ID: <87zh24611c.fsf@hajtower> References: <87sg7xxo0s.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="7256"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: 23461@debbugs.gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Dec 23 15:39:11 2020 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 1ks5IF-0001kx-1D for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 23 Dec 2020 15:39:11 +0100 Original-Received: from localhost ([::1]:55214 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ks5ID-0001q0-LO for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 23 Dec 2020 09:39:09 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37988) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ks5I6-0001pV-7d for bug-gnu-emacs@gnu.org; Wed, 23 Dec 2020 09:39:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:40807) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ks5I5-0007yU-Vy for bug-gnu-emacs@gnu.org; Wed, 23 Dec 2020 09:39:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ks5I5-0001ms-SB for bug-gnu-emacs@gnu.org; Wed, 23 Dec 2020 09:39:01 -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: Wed, 23 Dec 2020 14:39:01 +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.16087342826787 (code B ref 23461); Wed, 23 Dec 2020 14:39:01 +0000 Original-Received: (at 23461) by debbugs.gnu.org; 23 Dec 2020 14:38:02 +0000 Original-Received: from localhost ([127.0.0.1]:52353 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ks5H7-0001lJ-R7 for submit@debbugs.gnu.org; Wed, 23 Dec 2020 09:38:02 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]:40632) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ks5H5-0001l0-Pu for 23461@debbugs.gnu.org; Wed, 23 Dec 2020 09:38:01 -0500 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 34127160062 for <23461@debbugs.gnu.org>; Wed, 23 Dec 2020 15:37:53 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1608734273; bh=vg8X8DaC9a8GDIIIjLkVYrtZkNBBEO/j4UYn9m4PIvE=; h=From:To:Cc:Subject:Date:From; b=jUTZ8MLfIYOrkqjTD+usoS7zrv3/ysq0C7VK/NK3ldfl1PI5ianJzpu6447VUgMWQ uNFHZUj9oesixonBYL89oPWaLwo91n8siUQcfI8xdNUJHQNJDtXsJ64pnPs09aAh5L rMqBwKB3GrjPaBHenyTBdlUFyyMiHY1RzHLwR5cHqUVXFrFsfVtzN6CBAYK71ajbwY 5bMZH1oP23b+RouA+Zu/P/4zvc0psaPgcicvf0VDpeWIQ2GFpDN21OXf61tcPCXmtu O4Oo7LukYfShgUNoCYyvZ6J+f48ePnHLCS3FCqFmuie56TagGMUKseHPGAkE9eMrV6 unPKyHIScE7cQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4D1G5X24jgz6tmB; Wed, 23 Dec 2020 15:37:52 +0100 (CET) In-Reply-To: (Stefan Monnier's message of "Tue, 22 Dec 2020 23:00:01 -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:196617 Archived-At: --=-=-= Content-Type: text/plain Stefan Monnier writes: >> Investigating how to fix this leads to the longer story. There are two >> possible approaches: >> >> 1) use a string-style syntax (generic string) instead of c-style >> comments to flag HERE-documents. That way, font-lock picks up the >> correct face automagically. >> >> 2) Keep HERE_docs as c-style comments, but change the face mapping by >> injecting a function into font-lock-defaults which applies the >> string face to c-style comments. > > 3) Use `font-lock-syntactic-face-function`? Ah - thanks for this pointer! I wasn't aware of this function, though this feature is already in use in perl-mode.el. This looks like to be an improved variation of 2): HERE-docs remain marked as c-style comments, and `font-lock-syntactic-face-function` is used to display them as strings. A patch for this variation is attached. Tests and test resources are the same as with the first patch, and this patch also contains the same fix for HERE-doc starters with a trailing comment. -- 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: Display HERE-docs as strings >From f5025280db960b956e64bd9c1a7049c0fa294c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20J=C3=B6rg?= Date: Wed, 23 Dec 2020 15:17:27 +0100 Subject: [PATCH] ; perl-mode: Display here-docs as strings instead of comments. * lisp/progmodes/perl-mode.el (perl-font-lock-syntactic-face-function): Declare HERE-docs to be fontified as string. (perl-syntax-propertize-function): Handle comments after a HERE-doc starter line. * test/lisp/progmodes/cperl-mode-tests.el (cperl-test-heredocs): New test (30 should-forms) for various aspects of HERE-documents. Works for CPerl mode, and with the patch also for Perl mode. * test/lisp/progmodes/cperl-mode-resources/here-docs.pl: New file with test cases. --- lisp/progmodes/perl-mode.el | 20 +++- .../cperl-mode-resources/here-docs.pl | 111 ++++++++++++++++++ test/lisp/progmodes/cperl-mode-tests.el | 29 +++++ 3 files changed, 159 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 fd8a51b5a5..cc3eb4948a 100644 --- a/lisp/progmodes/perl-mode.el +++ b/lisp/progmodes/perl-mode.el @@ -324,13 +324,28 @@ 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 + ;; Before changing the syntax to c-style comment, let's + ;; check whether we are in an end-of-line comment, and + ;; if so, cheat by shifting the comment markers one char + ;; to the left. + (when (nth 4 (save-excursion (syntax-ppss eol))) + (when (equal (car (syntax-after (1- eol))) + (car (string-to-syntax "<"))) + ;; yet another edge case: "#" is the last character + ;; in that line, so there's actually no comment. + (put-text-property (- eol 2) (1- eol) + 'syntax-table (string-to-syntax "<"))) + (put-text-property (1- eol) eol + 'syntax-table (string-to-syntax ">"))) (cons (car (string-to-syntax "< c")) ;; Remember the names of heredocs found on this line. (cons (cons (pcase (aref name 0) @@ -485,6 +500,9 @@ perl-syntax-propertize-special-constructs (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 + 'font-lock-string-face) ((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..39e4fe06ba --- /dev/null +++ b/test/lisp/progmodes/cperl-mode-resources/here-docs.pl @@ -0,0 +1,111 @@ +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 as +'font-lock-string-face. 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'; + +=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 + +my $noindent = "This should be left-justified"; + +=head2 Test case 2 + +A HERE document followed by a continuation line + +=cut + +## test case + +$text = <