sheerun / vim-polyglot

A solid language pack for Vim.
5.58k stars 298 forks source link

Polyglot overwrite tab size for Fortran fixed format #592

Closed tapirpad closed 4 years ago

tapirpad commented 4 years ago

Does this bug happen when you install plugin without vim-polyglot? no

Describe the bug: Polyglot seems to overwrite the setting for tab size in neovim while editing fortran77 (fixed format) code. With polyglot enabled the tab size for fortran77 is 6. Removing the entire polyglot plugin fixed the issue. I tried disabling polyglot just for fortran with the following

let g:polyglot_disabled = ['fortran']

but it does not fixed the issue.

To Reproduce: Set tab size other than 6.

set tabstop=2

Install and enable polyglot, then try using tab in a fortran77 file.

sheerun commented 4 years ago

could you please send the file contents? or at least only first part of it until the first indentation?

sheerun commented 4 years ago

btw. the right disable is let g:polyglot_disabled = ['autoindent'] but I think I'll fix it very soon

sheerun commented 4 years ago

I see, it's probably something like this:

C AREA OF A TRIANGLE WITH A STANDARD SQUARE ROOT FUNCTION
C INPUT - TAPE READER UNIT 5, INTEGER INPUT
C OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUT
C INPUT ERROR DISPLAY ERROR OUTPUT CODE 1 IN JOB CONTROL LISTING
      READ INPUT TAPE 5, 501, IA, IB, IC

the solution is to implement different indentation detection : https://github.com/tpope/vim-sleuth/pull/53

also to preserve default set by filetype if there's not enough evidence

sheerun commented 4 years ago

I've limited autodetection of indent to 2, 3, 4 or 8. This should fix your issue

tapirpad commented 4 years ago

I tried updating the plugin. But the tab size is now 8 instead of 6. Probably because the autoindent cannot be 6 by your change?

In Fortran fixed format, all lines begin at column 7 but the indentation after that is usually small because each line is not allowed to have more than 72 columns. Is this what causes problem for the autoindent?

I tried let g:polyglot_disabled = ['autoindent']but it does not change anything either.

I will put an example of the code here.


************************************************************************
C
      subroutine CompFStrain(shg,ul,ndf,ndm,Fdef,detF,Cdef,Bdef,Edef)
C
C      Purpose: Compute internal force vector for 3-D element
C      Inputs:
C         BmatC   - matrix of gradient of shape Functions
C         Cmat    - matrix of material coefficient
C         ul      - solution vector
C         ndf     - no of degrees of freedom per node
C         nst     - number of element equations (ndf*#nodes)
C      Outputs:
C         SVoigt    - stress vector at integration point
C
************************************************************************
      implicit none

      include 'eldata.h'  ! dm,n,ma,mct,iel,nel

C     Subroutine input
      integer ndf, ndm
      real*8  shg(ndm+1,nel+1), ul(ndf,nel,6)
C     Subroutine output
      real*8  Fdef(ndm,ndm), detF
      real*8  Cdef(ndm,ndm), Bdef(ndm,ndm), Edef(ndm,ndm)
C     Local Variables
      integer i, j, k
      real*8  Imat(ndm,ndm)

************************************************************************
C     Compute deformation gradient, Fdef
      call pzero(Fdef,ndm*ndm)
      do k=1,nel
        do j=1,ndm
          do i=1,ndm
            Fdef(i,j) = Fdef(i,j) + ul(i,k,1)*shg(j,k)
          enddo
        enddo
      enddo

      Fdef(1,1) = Fdef(1,1) + 1
      Fdef(2,2) = Fdef(2,2) + 1
      Fdef(3,3) = Fdef(3,3) + 1

C     Compute determinant of Fdef (Jacobian)
      call det3(Fdef, detF)

C     Compute right elastic Cauchy-Green tensor
      call pzero(Cdef,ndm*ndm)
C      call DGEMM('T','N',3,3,3,1.d0,Fdef,3,Fdef,3,0.d0,Cdef,3)
      do j=1,ndm
        do k=1,ndm
          do i=1,ndm
            Cdef(i,j) = Cdef(i,j) + Fdef(k,i)*Fdef(k,j)
          enddo
        enddo
      enddo

C     Compute left elastic Cauchy-Green tensor
      call pzero(Bdef,ndm*ndm)
C      call DGEMM('N','T',3,3,3,1.d0,Fdef,3,Fdef,3,0.d0,Bdef,3)
      do j=1,ndm
        do k=1,ndm
          do i=1,ndm
            Bdef(i,j) = Bdef(i,j) + Fdef(i,k)*Fdef(j,k)
          enddo
        enddo
      enddo

C     Compute Lagrangian Strain Tensor, Edef
      call delta(Imat)
      call pzero(Edef,ndm*ndm)
      do j=1,ndm
        do i=1,ndm
          Edef(i,j) = Edef(i,j) + ( Cdef(i,j) - Imat(i,j) )/2.d0
        enddo
      enddo

      end
sheerun commented 4 years ago

TIL thank you I'll fix it again

sheerun commented 4 years ago

One more thing, could you please explain why you are setting tabstop to 2? Tabs are not used in this file

sheerun commented 4 years ago

let g:polyglot_disabled = ['autoindent'] should be at the top of .vimrc maybe that's why it doesn't work

sheerun commented 4 years ago

I fixed it two ways: allowed again for 6-spaces indent, and I do not set tabstop if someone changed it. Still, could you explain why tabstop=2? It should not matter because indent is with spaces anyway

sheerun commented 4 years ago

Okay it's not yet fixed, what probably should be detected is:

smarttab
shiftwidth=2
tabstop=2 (unless changed?)

I wonder if you use 5-spaces continuation indent?

tapirpad commented 4 years ago

One more thing, could you please explain why you are setting tabstop to 2? Tabs are not used in this file

That's because I convert my tab to space.

I found some fortran file where tab was used in the beginning. For this file, I got the tabsize=2 even with polyglot enabled. I put part of the content below.

!**********************************************************************
!
    subroutine NL_Elem2_2dKF(ndf,ndm,nel,nst,d,xl,
     &           ul,ElemK,ElemF)

!.... Program to calculate stiffness matrix and force vector for
!     nonlinear elasticity pure-displacement element
!
!**********************************************************************

    Implicit None

      integer         numnp,numel,nummat,nen,neq,ipr
      common /cdata/  numnp,numel,nummat,nen,neq,ipr

      logical debug
      common /debugs/ debug

      include 'comblk.h'
      include 'pointer.h'

!     Input Variables
    integer ndf,ndm,nel,nst
    real*8 d(*),xl(ndm,*),ul(ndf,nel,6)

!     Output Variables
        real*8 ElemK(nst,nst),ElemF(nst)

!     Local Variables
    integer i,j,k,l,iprob,lint,m

    real*8 w,det,Wgt,
     >       litr,lits,dix,diy,Bmat(4,nst),F(2,2),JxX,
     >       thick,fi(2,2),
     >       shl(1,nen),shld(2,nen),shls(3,nen),
     >       shg(2,nen),Qxy(2,nen)
    real*8 bubble(3),sx(2,2),shgs(3,nen),tempk
    integer ib
    logical der,bf

    real*8 lam,two,four

    real*8  DB(4,nst),Smat(4,4),cmat(4,4),sigma3(4)
        logical dprt

      two = 2.d0
      four = two*two

      call pzero(ElemK,nst*nst)
      call pzero(ElemF,nst)
      call pzero(Bmat,4*nst)

      call M_Prop(d,lam,iprob)

      thick = 1.d0

      ! Load Guass Integration Points

      Call IntPoint(nel,lint)

      if(debug) then
        dprt = .false.
        if(dprt) then

          write(24,'(A,I4,A,I4)') 'lint ',lint,' iprob = ',iprob
          write(24,'(A,f17.7)') 'lam ',lam
          WRITE(24,'(A)') 'xl'
          do j = 1,nel
            WRITE(24,*) (xl(i,j),i=1,ndm)
          enddo   
          WRITE(24,'(A)') 'ul'
          do j = 1,nel
            WRITE(24,*) (ul(i,j,1),i=1,ndf)
          enddo   

        endif
      endif

      ib = 0
      der = .false.
      bf = .false.

      !Integration Loop
      do 10 l = 1,lint

         !Evaluate first derivatives of basis functions at int. point
        if(nel.eq.3.or.nel.eq.6) then
          call intpntt(l,lint,ib,Wgt,litr,lits)
          call shlt(litr,lits,nel,nen,der,bf,shl,shld,shls,bubble)
          call shgt(xl,nel,shld,shls,nummat,nen,bf,der,det,shg,shgs,
     &                bubble,sx)
        elseif(nel.eq.4.or.nel.eq.9) then
          call intpntq(l,lint,ib,Wgt,litr,lits)
          call shlq(litr,lits,nel,nen,der,bf,shl,shld,shls,bubble)
          call shgq(xl,nel,shld,shls,nummat,nen,bf,der,det,shg,shgs,
     &                bubble,sx)
         endif
      enddo
      end

let g:polyglot_disabled = ['autoindent'] should be at the top of .vimrc maybe that's why it doesn't work

Yes, It works if I put that at the top off .vimrc

Okay it's not yet fixed, what probably should be detected is:

smarttab
shiftwidth=2
tabstop=2 (unless changed?)

I do have those three setting.

I wonder if you use 5-spaces continuation indent?

No.

For now, I will just disable the autoindent for fortran. But let me know if you want me to try things out.

sheerun commented 4 years ago

what settings you are expecing to be recognized for file above? currently it's expandtab, ts=2, sw=2 which seems ok

tapirpad commented 4 years ago

Sorry, I just wanted to mention that in some Fortran file (the first example, with no tabs, only spaces) the tab setting was overwritten to be 6 but in another Fortran file (the second one, with some tabs in the beginning) the tab setting is ok.

sheerun commented 4 years ago

Not only vim-polyglot now recognizes manually-set shiftwidth, but also should properly detect indent in both cases

tigerinus commented 3 years ago

@sheerun - your commit 6422a5a479905bf32abe7a322d1c0b0a75d4aa8c breaks my nvim (0.3.4) on Debian Buster:

image

This would fix the problem. Can you take a look?

diff --git a/ftdetect/polyglot.vim b/ftdetect/polyglot.vim
index 2ebfec9..cc1f423 100644
--- a/ftdetect/polyglot.vim
+++ b/ftdetect/polyglot.vim
@@ -2645,7 +2645,7 @@ if !has_key(s:disabled_packages, 'autoindent')
     let max_count = 0
     let final_counts = {}
     for [indent, indent_count] in items(a:indents)
-      let indent_count *= 1.5
+      let indent_count = indent_count * 1.5
       for [indent2, indent2_count] in items(a:indents)
         if indent2 > indent && indent2 % indent == 0
           let indent_count += indent2_count

Filed a separate bug https://github.com/sheerun/vim-polyglot/issues/601