By the principle of superposition, a state vector can be defined as $$\begin{align} \psi(x) &= c_1 \psi_1(x) + c_2 \psi_2(x) + \cdots + c_n \psi_n(x) \\ \lvert\psi\rangle &= \begin{pmatrix}c_1 \\ c_2 \\ \vdots \\ c_n\end{pmatrix} = c_1\lvert\varepsilon_1\rangle + c_2\lvert\varepsilon_2\rangle + \cdots + c_n\lvert\varepsilon_n\rangle \end{align}$$ where $c_n = \langle \varepsilon_n\lvert \psi\rangle$. Also, the state vector can represent a wave function in a continuous case as in $$\begin{align} \lvert\psi\rangle &= \begin{pmatrix}\psi(x\to -\infty) \\ \vdots \\ \psi(x=0) \\ \vdots \\ \psi(x = 0.000\ldots 01) \\ \psi(x = 0.000\ldots 02) \\ \vdots \\ \psi(x\to\infty)\end{pmatrix} \tag{1} \\ &= \psi(x\to -\infty)\lvert x\to -\infty\rangle + \cdots + \psi(x = 0)\lvert x=0\rangle + \cdots \end{align}$$
My first question is, in the sequence of expressions $$\begin{align} \langle\psi\lvert\psi\rangle &= \int_{-\infty}^{+\infty}\psi^{*}(x)\psi(x)\ \mathrm{d}x \\ &= \int_{-\infty}^{+\infty}\langle x\lvert\psi\rangle^* \langle x\lvert\psi\rangle\ \mathrm{d}x \\ &= \int_{-\infty}^{+\infty}\langle\psi\lvert x\rangle \langle x\lvert\psi\rangle\ \mathrm{d}x \\ &= \color{red}{\langle\psi\lvert}\int_{-\infty}^{+\infty}\lvert x\rangle \langle x\rvert\ \mathrm{d}x\ \color{red}{\lvert\psi\rangle} \\ &= \langle\psi\rvert \hat{\mathbb{1}}\lvert\psi\rangle \end{align}$$ with $\hat{\mathbb{1}} = \int_{-\infty}^{+\infty}\mathrm{d}x\ \lvert x\rangle\langle x\rvert$, why can the state vector can be pulled out from the integral?
I have an idea but I think it should be wrong:
As shown in equation (1), the state vector is a set of values such that each element is the value of the wave function, e.g. $\psi(0)$, $\psi(0.002)$, etc. Thus, it can be considered as a constant since each element will never change. Therefore, it can be pulled out from the integral.
However, this idea fails to explain the observable case. Consider an operator which contains a momentum component, a differential operator, $\hat{Q}(x, \frac{\hbar}{i}\frac{\mathrm{d}}{\mathrm{d}x})$. The expectation value of this operator is $$\begin{align} \langle\hat{Q}\rangle &= \int_{-\infty}^{+\infty}\psi^*(x)\hat{Q}\psi(x)\ \mathrm{d}x \\ &= \int_{-\infty}^{+\infty}\langle x\lvert\psi\rangle^*\hat{Q}\langle x\lvert\psi\rangle\ \mathrm{d}x \\ &= \int_{-\infty}^{+\infty}\langle\psi\lvert x\rangle\hat{Q}\langle x\lvert\psi\rangle\ \mathrm{d}x \\ &= \color{red}{\langle\psi\rvert}\int_{-\infty}^{+\infty}\lvert x\rangle\hat{Q}\langle x\rvert\ \mathrm{d}x\ \color{red}{\lvert\psi\rangle} \\ &= \langle\psi\rvert\int_{-\infty}^{+\infty}\mathrm{d}x\ \lvert x\rangle\langle x\rvert\color{blue}{\hat{Q}}\lvert\psi\rangle \tag{2} \\ &= \langle\psi\rvert\hat{\mathbb{1}}\hat{Q}\lvert\psi\rangle \\ &= \langle\psi\rvert\hat{Q}\lvert\psi\rangle \end{align}$$ If the state vector is constant, it should give zero when a momentum operator acts on it, and the Dirac representation will fail.
So, if it is not a constant, how can it be pulled out from the integral?
My second question is that, in equation (2), why can the operator $\hat{Q}$ skip the $\langle x\rvert$ vector and act on the state vector?