Implementing indentation correctly, in any editor, is hard. Here's an Emacs maintainer's summary of the process: https://github.com/emacs-mirror/emacs/blob/51e7e463e93708a0e40688f91200e9e9869ec662/lisp/emacs-lisp/smie.el#L61